package top.continew.admin.controller.system;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.business.model.query.ModelQuery;
import top.continew.admin.business.model.req.DeptModelReq;
import top.continew.admin.business.model.req.DeptPlatformReq;
import top.continew.admin.business.model.resp.ModelResp;
import top.continew.admin.business.service.DeptModelService;
import top.continew.admin.business.service.DeptPlatformService;
import top.continew.admin.system.model.query.DeptQuery;
import top.continew.admin.system.model.req.DeptReq;
import top.continew.admin.system.model.resp.DeptResp;
import top.continew.admin.system.service.DeptService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * 部门管理 API
 *
 * @author Charles7c
 * @since 2023/1/22 17:50
 */
@Tag(name = "部门管理 API")
@Valid
@RestController
@CrudRequestMapping(value = "/system/dept", api = {Api.TREE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
@RequiredArgsConstructor
public class DeptController extends BaseController<DeptService, DeptResp, DeptResp, DeptQuery, DeptReq> {

    private final DeptModelService deptModelService;

    private final DeptPlatformService deptPlatformService;

    @Operation(summary = "部门与操作平台绑定", description = "部门与操作平台绑定")
    @PutMapping("/bind/platform")
    public R bindPlatform(@Validated @RequestBody DeptPlatformReq req) {
        deptPlatformService.bind(req);
        return R.ok();
    }

    @Operation(summary = "部门与模型绑定", description = "部门与模型绑定")
    @PutMapping("/bind/model")
    public R bindPlatform(@Validated @RequestBody DeptModelReq req) {
        deptModelService.bind(req);
        return R.ok();
    }

    @Operation(summary = "获取部门绑定的操作平台", description = "获取部门绑定的操作平台")
    @GetMapping("/getPlatformByDept")
    public R getPlatformByDept(@NotNull(message = "操作平台列表不能为空") Long deptId) {
        List<String> res = deptPlatformService.getPlatformByDept(deptId);
        return R.ok(res);
    }

    @Operation(summary = "获取部门绑定的模型ID", description = "获取部门绑定的模型ID")
    @GetMapping("/getModelByDept")
    public R getModelByDept(@NotNull(message = "操作平台列表不能为空") Long deptId) {
        List<Integer> res = deptModelService.getModelByDept(deptId);
        return R.ok(res);
    }

    @Operation(summary = "获取用户可见的操作平台", description = "获取用户可见的操作平台")
    @GetMapping("/getPlatform")
    public R getPlatforms() {
        return R.ok(deptPlatformService.getPlatforms());
    }

    @Operation(summary = "获取用户可见模型", description = "获取用户可见模型")
    @GetMapping("/getModels")
    public PageResp<ModelResp> getModels(ModelQuery query, @Validated PageQuery pageQuery) {
        return deptModelService.getModels(query, pageQuery);
    }
}
