package com.tashow.cloud.ai.controller.admin.model;

import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleRespVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelAccuracyAnalysisReqVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelAnalysisResultVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelDifferenceVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelIterateReqVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelPageReqVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelReferenceSamplesReqVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelRespVO;
import com.tashow.cloud.ai.controller.admin.model.vo.AiModelSaveReqVO;
import com.tashow.cloud.ai.dal.dataobject.aisample.AiSampleDO;
import com.tashow.cloud.ai.dal.dataobject.model.AiModelDO;
import com.tashow.cloud.ai.service.aisample.AiSampleService;
import com.tashow.cloud.ai.service.model.AiModelService;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - AI模型管理")
@RestController
@RequestMapping("/ai/model")
@Validated
public class AiModelController {

    @Resource
    private AiModelService modelService;
    @Resource
    private AiSampleService aiSampleService;

    @PostMapping("/create")
    @Operation(summary = "创建模型")
    @PermitAll
    public CommonResult<Long> createModel(@Valid @RequestBody AiModelSaveReqVO createReqVO) {
        return success(modelService.createModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型")
    @PermitAll
    public CommonResult<Boolean> updateModel(@Valid @RequestBody AiModelSaveReqVO updateReqVO) {
        modelService.updateModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteModel(@RequestParam("id") Long id) {
        modelService.deleteModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取所有已启用的模型")
    @PermitAll
    public CommonResult<List<AiModelRespVO>> getEnabledModels() {
        List<AiModelDO> models = modelService.getEnabledModels();
        return success(BeanUtils.toBean(models, AiModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型分页")
    @PermitAll
    public CommonResult<PageResult<AiModelRespVO>> getModelPage(@Valid AiModelPageReqVO pageReqVO) {
        PageResult<AiModelDO> pageResult = modelService.getModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiModelRespVO.class));
    }

    @PostMapping("/reference-samples")
    @Operation(summary = "模型引用样本")
    @PermitAll
    public CommonResult<Boolean> referenceSamples(@Valid @RequestBody AiModelReferenceSamplesReqVO reqVO) {
        modelService.referenceSamples(reqVO.getModelId(), reqVO.getSampleIds());
        return success(true);
    }

    @GetMapping("/samples")
    @Operation(summary = "获取模型关联样本")
    @Parameter(name = "modelId", description = "模型ID", required = true)
    @PermitAll
    public CommonResult<List<AiSampleRespVO>> getModelSamples(@RequestParam Long modelId) {
        List<AiSampleDO> samples = modelService.getModelSamples(modelId);
        return success(samples.stream().map(aiSampleService::buildSampleRespVO).toList());
    }

    @PostMapping("/accuracy-analysis")
    @Operation(summary = "模型准确度分析")
    @PermitAll
    public CommonResult<AiModelAnalysisResultVO> analyzeModel(@Valid @RequestBody AiModelAccuracyAnalysisReqVO reqVO) {
        return success(modelService.analyzeModel(reqVO.getSampleIds(), reqVO.getModelIds()));
    }

    @GetMapping("/difference")
    @Operation(summary = "模型样本差异筛查")
    @PermitAll
    public CommonResult<AiModelDifferenceVO> checkDifference(@RequestParam List<Long> modelIds) {
        return success(modelService.checkDifference(modelIds));
    }

    @PostMapping("/iterate")
    @Operation(summary = "模型迭代")
    @PermitAll
    public CommonResult<Long> iterateModel(@Valid @RequestBody AiModelIterateReqVO reqVO) {
        return success(modelService.iterateModel(reqVO.getModelId(), reqVO.getNewVersion(), reqVO.getDescription()));
    }
}

