package top.continew.admin.controller.tool;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.generator.model.entity.FieldConfigDO;
import top.continew.admin.generator.model.entity.GenConfigDO;
import top.continew.admin.generator.model.query.TableQuery;
import top.continew.admin.generator.model.req.GenConfigReq;
import top.continew.admin.generator.model.resp.GeneratePreviewResp;
import top.continew.admin.generator.model.resp.TableResp;
import top.continew.admin.generator.service.GeneratorService;
import top.continew.admin.system.service.DictService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.sql.SQLException;
import java.util.List;

/**
 * 代码生成 API
 *
 * @author Charles7c
 * @since 2023/8/3 22:58
 */
@Tag(name = "代码生成 API")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/generator")
public class GeneratorController {

    private final GeneratorService baseService;
    private final DictService dictService;

    @Operation(summary = "分页查询数据表", description = "分页查询数据表")
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/table")
    public PageResp<TableResp> pageTable(TableQuery query, @Validated PageQuery pageQuery) throws SQLException {
        return baseService.pageTable(query, pageQuery);
    }

    @Operation(summary = "查询生成配置信息", description = "查询生成配置信息")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/config/{tableName}")
    public GenConfigDO getGenConfig(@PathVariable String tableName) throws SQLException {
        return baseService.getGenConfig(tableName);
    }

    @Operation(summary = "查询字段配置列表", description = "查询字段配置列表")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @Parameter(name = "requireSync", description = "是否需要同步", example = "false", in = ParameterIn.QUERY)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/field/{tableName}")
    public List<FieldConfigDO> listFieldConfig(@PathVariable String tableName,
                                               @RequestParam(required = false, defaultValue = "false") Boolean requireSync) {
        return baseService.listFieldConfig(tableName, requireSync);
    }

    @Operation(summary = "保存配置信息", description = "保存配置信息")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @PostMapping("/config/{tableName}")
    public void saveConfig(@Validated @RequestBody GenConfigReq req, @PathVariable String tableName) {
        baseService.saveConfig(req, tableName);
    }

    @Operation(summary = "生成预览", description = "预览生成代码")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/preview/{tableName}")
    public List<GeneratePreviewResp> preview(@PathVariable String tableName) {
        return baseService.preview(tableName);
    }

    @Operation(summary = "生成代码", description = "生成代码")
    @Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
    @SaCheckPermission("tool:generator:list")
    @PostMapping("/{tableNames}")
    public void generate(@PathVariable List<String> tableNames, HttpServletResponse response) {
        baseService.generate(tableNames, response);
    }

    @Operation(summary = "查询字典", description = "查询字典列表")
    @SaCheckPermission("tool:generator:list")
    @GetMapping("/dict")
    public List<LabelValueResp> listDict() {
        List<LabelValueResp> dictList = dictService.listDict(null, null);
        dictList.addAll(dictService.listEnumDict());
        return dictList;
    }
}
