package com.huangzj.protoplugin.mysql;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/** 表名/列名到合法 Java 标识符的转换。 */
public final class JavaNames {

    private static final Set<String> KEYWORDS = new HashSet<>();

    static {
        String[] k =
                ("abstract assert boolean break byte case catch char class const "
                                + "continue default do double else enum extends final finally float "
                                + "for goto if implements import instanceof int interface long native "
                                + "new package private protected public return short static strictfp "
                                + "super switch synchronized this throw throws transient try void "
                                + "volatile while true false null record sealed permits yields")
                        .split("\\s+");
        for (String s : k) {
            KEYWORDS.add(s);
        }
    }

    private JavaNames() {}

    public static String toClassNameFromTable(String tableName) {
        String pascal = toPascalCase(splitIdentifiers(tableName));
        return pascal.isEmpty() ? "Entity" : pascal;
    }

    public static String toFieldName(String columnName) {
        String camel = toCamelCase(splitIdentifiers(columnName));
        if (camel.isEmpty()) {
            return "field";
        }
        camel = Character.toLowerCase(camel.charAt(0)) + camel.substring(1);
        return safeIdentifier(camel);
    }

    public static String toPascalCaseField(String columnName) {
        String camel = toCamelCase(splitIdentifiers(columnName));
        if (camel.isEmpty()) {
            return "Field";
        }
        return Character.toUpperCase(camel.charAt(0)) + camel.substring(1);
    }

    private static String safeIdentifier(String name) {
        if (KEYWORDS.contains(name.toLowerCase(Locale.ROOT))) {
            return name + "Value";
        }
        return name;
    }

    private static String[] splitIdentifiers(String raw) {
        if (raw == null || raw.isEmpty()) {
            return new String[0];
        }
        String s = raw.replace('`', ' ').trim();
        return s.split("[_\\s-]+");
    }

    private static String toCamelCase(String[] parts) {
        StringBuilder sb = new StringBuilder();
        for (String p : parts) {
            if (p == null || p.isEmpty()) {
                continue;
            }
            String low = p.toLowerCase(Locale.ROOT);
            if (sb.length() == 0) {
                sb.append(low);
            } else {
                if (low.isEmpty()) {
                    continue;
                }
                sb.append(Character.toUpperCase(low.charAt(0)));
                if (low.length() > 1) {
                    sb.append(low, 1, low.length());
                }
            }
        }
        return sb.toString();
    }

    private static String toPascalCase(String[] parts) {
        String camel = toCamelCase(parts);
        if (camel.isEmpty()) {
            return "";
        }
        return Character.toUpperCase(camel.charAt(0)) + camel.substring(1);
    }
}
