package com.qqchen.deploy.backend.framework.generator;

import com.qqchen.deploy.backend.framework.generator.CodeGeneratorUtils.GeneratorConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
 * 代码生成器主入口
 */
@Slf4j
public class CodeGenerator {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        try {
            // 0. 选择API类型
            System.out.println("请选择API类型（1: 三方API, 2: 二方API）：");
            String apiType = scanner.nextLine();
            boolean isThirdPartyApi = "1".equals(apiType);
            
            // 1. 输入模块信息
            System.out.println("请输入模块名称（中文，如：用户）：");
            String moduleName = scanner.nextLine();
            
            System.out.println("请输入类名（英文，如：User）：");
            String className = scanner.nextLine();
            
            System.out.println("请输入基础包路径（如：com.qqchen.deploy.backend.system）：");
            String basePackage = scanner.nextLine();
            
            System.out.println("请输入输出路径（如：src/main/java）：");
            String outputPath = scanner.nextLine();
            
            System.out.println("请输入表名（如：sys_user）：");
            String tableName = scanner.nextLine();
            
            System.out.println("请输入URL路径（如：user）：");
            String urlPath = scanner.nextLine();
            
            // 2. 输入建表SQL
            System.out.println("请输入CREATE TABLE SQL语句（以分号结束）：");
            StringBuilder sqlBuilder = new StringBuilder();
            String line;
            while (!(line = scanner.nextLine()).contains(";")) {
                sqlBuilder.append(line).append("\n");
            }
            sqlBuilder.append(line);
            String createTableSql = sqlBuilder.toString();

            // 3. 验证输入
            if (StringUtils.isAnyBlank(moduleName, className, basePackage, outputPath, tableName, urlPath, createTableSql)) {
                throw new IllegalArgumentException("所有输入项都不能为空");
            }

            // 4. 配置生成器
            GeneratorConfig config = new GeneratorConfig();
            config.setModuleName(moduleName);
            config.setClassName(className);
            config.setBasePackage(basePackage);
            config.setOutputPath(outputPath);
            config.setTableName(tableName);
            config.setUrlPath(urlPath);
            config.setThirdPartyApi(isThirdPartyApi);

            // 5. 生成代码
            CodeGeneratorUtils.generateCode(createTableSql, config);
            
            System.out.println("代码生成成功！");
            System.out.println("生成的文件路径：" + outputPath);
            
        } catch (Exception e) {
            log.error("代码生成失败", e);
            System.err.println("代码生成失败：" + e.getMessage());
        } finally {
            scanner.close();
        }
    }

    /**
     * 通过API方式生成代码
     */
    public static void generate(String moduleName, String className, String basePackage,
                              String outputPath, String tableName, String urlPath, String createTableSql,
                              boolean isThirdPartyApi) {
        try {
            // 1. 验证输入
            if (StringUtils.isAnyBlank(moduleName, className, basePackage, outputPath, tableName, urlPath, createTableSql)) {
                throw new IllegalArgumentException("所有输入项都不能为空");
            }

            // 2. 配置生成器
            GeneratorConfig config = new GeneratorConfig();
            config.setModuleName(moduleName);
            config.setClassName(className);
            config.setBasePackage(basePackage);
            config.setOutputPath(outputPath);
            config.setTableName(tableName);
            config.setUrlPath(urlPath);
            config.setThirdPartyApi(isThirdPartyApi);

            // 3. 生成代码
            CodeGeneratorUtils.generateCode(createTableSql, config);
            
            log.info("代码生成成功！输出路径：{}", outputPath);
        } catch (Exception e) {
            log.error("代码生成失败", e);
            throw new RuntimeException("代码生成失败：" + e.getMessage(), e);
        }
    }
} 