package cn.stylefeng.team.form.modular.manage.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.form.api.entity.FormDefinition;
import cn.stylefeng.team.form.api.entity.FormModel;
import cn.stylefeng.team.form.modular.manage.request.FormModelRequest;
import cn.stylefeng.team.form.modular.manage.service.FormModelService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "表单-模型")
public class FormModelController {

    @Resource
    private FormModelService formModelService;

    @GetResource(name = "获取模型列表（带分页）", path = "/formModel/page")
    public ResponseData<PageResult<FormModel>> page(@Validated(BaseRequest.page.class) FormModelRequest formModelRequest) {
        return new SuccessResponseData<>(formModelService.findPage(formModelRequest));
    }

    @GetResource(name = "获取表单模型列表，一般用在通用表单选择界面", path = "/formModel/customFindPage")
    public ResponseData<PageResult<FormModel>> customFindPage(FormModelRequest formModelRequest) {
        return new SuccessResponseData<>(formModelService.findPage(formModelRequest));
    }

    @PostResource(name = "添加表单模型", path = "/formModel/add")
    public ResponseData<FormModel> add(@RequestBody @Validated(FormModelRequest.add.class) FormModelRequest formModelRequest) {
        formModelService.add(formModelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "删除表单模型", path = "/formModel/delete")
    public ResponseData<?> delete(@RequestBody @Validated(FormModelRequest.delete.class) FormModelRequest formModelRequest) {
        formModelService.del(formModelRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "编辑表单模型", path = "/formModel/edit")
    public ResponseData<?> edit(@RequestBody @Validated(FormModelRequest.edit.class) FormModelRequest formModelRequest) {
        formModelService.edit(formModelRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "查看详情", path = "/formModel/detail")
    public ResponseData<FormModel> detail(@Validated(FormModelRequest.detail.class) FormModelRequest formModelRequest) {
        return new SuccessResponseData<>(formModelService.detail(formModelRequest));
    }

    @PostResource(name = "升级表单模型", path = "/formModel/upgradeModel")
    public ResponseData<FormDefinition> upgradeModel(
            @RequestBody @Validated(FormModelRequest.upgradeModel.class) FormModelRequest formModelRequest) {
        FormDefinition formDefinition = formModelService.upgradeModel(formModelRequest);
        return new SuccessResponseData<>(formDefinition);
    }

    @GetResource(name = "获取表单下拉列表", path = "/formModel/getSelectList")
    public ResponseData<List<FormModel>> getSelectList(FormModelRequest formModelRequest) {
        List<FormModel> formModelList = formModelService.getSelectList(formModelRequest);
        return new SuccessResponseData<>(formModelList);
    }

    @GetResource(name = "复制表单模型", path = "/formModel/copyModel")
    public ResponseData<?> copyModel(@Validated(FormModelRequest.copyModel.class) FormModelRequest formModelRequest) {
        formModelService.copyModel(formModelRequest);
        return new SuccessResponseData<>();
    }

}
