package cn.stylefeng.team.meta.modular.controller;

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.team.generator.api.pojo.generator.result.CommonGenerateResult;
import cn.stylefeng.team.meta.api.entity.MetaDefinition;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.meta.modular.pojo.CategoryInfo;
import cn.stylefeng.team.meta.modular.pojo.request.DatabaseOperateRequest;
import cn.stylefeng.team.meta.modular.pojo.request.MetaModelRequest;
import cn.stylefeng.team.meta.modular.pojo.request.SqlExportRequest;
import cn.stylefeng.team.meta.modular.pojo.request.TableUpdateOperateRequest;
import cn.stylefeng.team.meta.modular.service.MetaModelService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "数据表")
public class MetaModelController {

    @Resource
    private MetaModelService metaModelService;

    @GetResource(name = "获取模型列表（带分页）", path = "/metaModel/page")
    public ResponseData<PageResult<MetaModel>> page(@Validated(BaseRequest.page.class) MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.findPage(metaModelRequest));
    }

    @GetResource(name = "获取业务下的所有数据表", path = "/metaModel/list")
    public ResponseData<List<MetaModel>> list(@Validated(BaseRequest.page.class) MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.findList(metaModelRequest));
    }

    @GetResource(name = "获取所有业务下的table", path = "/metaModel/getTotalTableList")
    public ResponseData<List<MetaModel>> getTotalTableList(MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.getTotalTableList(metaModelRequest));
    }

    @PostResource(name = "添加模型", path = "/metaModel/add")
    public ResponseData<MetaModel> add(@RequestBody @Validated(MetaModelRequest.add.class) MetaModelRequest metaModelRequest) {
        metaModelService.add(metaModelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "删除模型", path = "/metaModel/delete")
    public ResponseData<?> delete(@RequestBody @Validated(MetaModelRequest.delete.class) MetaModelRequest metaModelRequest) {
        metaModelService.del(metaModelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "批量删除模型", path = "/metaModel/batchDelete")
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) MetaModelRequest metaModelRequest) {
        metaModelService.batchDelete(metaModelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "编辑模型", path = "/metaModel/edit")
    public ResponseData<?> edit(@RequestBody @Validated(MetaModelRequest.edit.class) MetaModelRequest metaModelRequest) {
        metaModelService.edit(metaModelRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "查看模型详情", path = "/metaModel/detail")
    public ResponseData<MetaModel> detail(@Validated(MetaModelRequest.detail.class) MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.detail(metaModelRequest));
    }

    @PostResource(name = "升级模型", path = "/metaModel/upgradeModel", requiredPermission = true, requirePermissionCode = "META_UPDATE_AND_SAVE")
    public ResponseData<MetaDefinition> upgradeModel(
            @RequestBody @Validated(MetaModelRequest.upgradeModel.class) MetaModelRequest metaModelRequest) {
        MetaDefinition metaDefinition = metaModelService.upgradeModel(metaModelRequest);
        return new SuccessResponseData<>(metaDefinition);
    }

    @GetResource(name = "获取指定业务下所有的业务模型和字段", path = "/metaModel/getTotalModelAndFieldList")
    public ResponseData<List<MetaModel>> getTotalModelAndFieldList(
            @Validated(MetaModelRequest.getTotalModelAndFieldList.class) MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.getTotalModelAndFieldList(metaModelRequest));
    }

    @GetResource(name = "创建指定模型的sql", path = "/metaModel/createTableSql")
    public ResponseData<List<CommonGenerateResult>> createTableSql(@Validated SqlExportRequest sqlExportRequest) {
        return new SuccessResponseData<>(metaModelService.generateCreateTable(sqlExportRequest.getModelId()));
    }

    @PostResource(name = "创建表到数据库", path = "/metaModel/createTableIntoDb", requiredPermission = true, requirePermissionCode = "META_CREATE_TABLE")
    public ResponseData<?> createTableIntoDb(@RequestBody @Validated DatabaseOperateRequest dateDatabaseOperateRequest) {
        metaModelService.createTableIntoDb(dateDatabaseOperateRequest.getDefinitionId());
        return new SuccessResponseData<>();
    }

    @PostResource(name = "增量更新表到数据库", path = "/metaModel/updateTableIntoDb", requiredPermission = true, requirePermissionCode = "META_UPDATE_TABLE")
    public ResponseData<?> updateTableIntoDb(@RequestBody @Validated TableUpdateOperateRequest tableUpdateOperateRequest) {
        metaModelService.updateTableIntoDb(tableUpdateOperateRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "获取业务分类，和业务分类下的所有表，组成的树结构", path = "/metaModelCategory/getCategoryAndModelTree")
    public ResponseData<List<CategoryInfo>> getCategoryAndModelTree(MetaModelRequest metaModelRequest) {
        return new SuccessResponseData<>(metaModelService.getCategoryModelList(metaModelRequest));
    }

    @GetResource(name = "获取业务分类，和业务分类下的所有表，组成的树结构", path = "/metaModelCategory/validateTableExisted")
    public ResponseData<Boolean> validateTableExisted(@RequestParam("tableName") String tableName) {
        return new SuccessResponseData<>(metaModelService.validateTableExisted(tableName));
    }

}
