package com.auth.module.generator.service.helper;

import com.auth.module.generator.utils.CamelCaseNameConvertUtil;
import com.google.common.base.CaseFormat;

/**
 * 代码生成工具类
 */
public class VmsGeneratorPathHelper {

    /**
     * 处理模板文件路径和命名
     *
     * @param path      原始模板路径
     * @param tableName 数据库表名
     * @return 处理后的文件路径
     */
    public static String processVmPath(String path, String tableName) {
        String lowerCamelCase = CamelCaseNameConvertUtil.convertToCamelCase(tableName, false);

        if (lowerCamelCase != null) {
            String[] pathParts = path.replace("$className", lowerCamelCase).split("/");
            // 处理文件名
            pathParts[pathParts.length - 1] = processFilename(
                    pathParts[pathParts.length - 1],
                    lowerCamelCase
            );

            return String.join("/", pathParts);
        }

        return path;
    }

    /**
     * 处理文件名生成
     */
    private static String processFilename(String filename, String tableName) {
        filename = filename.replace(".vm", "");
        String[] parts = filename.split("\\.");
        String baseName = parts[0];
        String extension = parts.length > 1 ? parts[1] : "";

        String upperCamelCase = CamelCaseNameConvertUtil.convertToCamelCase(tableName, true);
        String lowerCamelCase = CamelCaseNameConvertUtil.convertToCamelCase(tableName, false);

        // 💡含Java和xml需要进行处理
        if (filename.contains(".java") || filename.contains(".xml")) {
            String toUpperCamelCase = CamelCaseNameConvertUtil.convertToCamelCase(baseName, true);
            return upperCamelCase + toUpperCamelCase + "." + extension;
        }

        // 💡前端配置
        return switch (filename) {
            case "api.ts", "store.ts" -> lowerCamelCase + ".ts";
            case "types.ts" -> lowerCamelCase + "DataType.ts";
            case "dialog.vue" -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamelCase) + "-dialog.vue";
            case "columns.tsx" -> lowerCamelCase + "-columns.tsx";
            default -> filename;
        };

    }
}