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

import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleFileRespVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSamplePageReqVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleRelateModelVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleRelateTagVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleRespVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleSaveReqVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.FileUploadReqVO;
import com.tashow.cloud.ai.dal.dataobject.aisample.AiSampleDO;
import com.tashow.cloud.ai.service.aisample.AiSampleService;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
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.servlet.http.HttpServletResponse;
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.io.IOException;
import java.util.List;

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

@Tag(name = "管理后台 - 样本库")
@RestController
@RequestMapping("/ai/sample")
@Validated
public class AiSampleController {

    @Resource
    private AiSampleService aiSampleService;

    @PostMapping("/create")
    @Operation(summary = "创建样本库-上传文件")
    @PermitAll
    public CommonResult<List<AiSampleFileRespVO>> createAiSample(FileUploadReqVO uploadReqVO) {
        return success(aiSampleService.createAiSample(uploadReqVO));
    }

    @PutMapping("/updates")
    @Operation(summary = "批量更新样本")
    @PermitAll
    public CommonResult<Boolean> updateAiSample(@Valid @RequestBody List<AiSampleSaveReqVO> updateReqVO) {
        aiSampleService.updateAiSamples(updateReqVO);
        return success(true);
    }

    @PutMapping("/relate")
    @Operation(summary = "添加关联标签")
    @PermitAll
    public CommonResult<Boolean> relate(@Valid @RequestBody AiSampleRelateTagVO relateTagVO) {
        aiSampleService.relate(relateTagVO);
        return success(true);
    }

    @DeleteMapping("/deleteRelate")
    @Operation(summary = "删除关联标签")
    @PermitAll
    public CommonResult<Boolean> deleteRelate(@Valid @RequestBody AiSampleRelateTagVO relateTagVO) {
        aiSampleService.deleteRelate(relateTagVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样本")
    @Parameter(name = "id", description = "编号", required = true)
    @PermitAll
    public CommonResult<Boolean> deleteAiSample(@RequestParam("id") String ids) {
        aiSampleService.deleteAiSample(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<AiSampleRespVO> getAiSample(@RequestParam("id") Long id) {
        return success(aiSampleService.buildSampleRespVO(aiSampleService.getAiSample(id)));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样本分页")
    @PermitAll
    public CommonResult<PageResult<AiSampleRespVO>> getAiSamplePage(@Valid AiSamplePageReqVO pageReqVO) {
        PageResult<AiSampleDO> pageResult = aiSampleService.getAiSamplePage(pageReqVO);
        List<AiSampleRespVO> list = pageResult.getList().stream().map(aiSampleService::buildSampleRespVO).toList();
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/download")
    @Operation(summary = "批量下载样本")
    @Parameter(name = "ids", description = "样本ID列表，逗号分隔", required = true, example = "1,2,3")
    @PermitAll
    public void downloadSamples(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        aiSampleService.downloadSamples(ids, response);
    }

    @PutMapping("/relateModel")
    @Operation(summary = "样本关联模型")
    @PermitAll
    public CommonResult<Boolean> relateModel(@Valid @RequestBody AiSampleRelateModelVO relateModelVO) {
        aiSampleService.relateSamples(relateModelVO.getSampleIds(), relateModelVO.getModelIds());
        return success(true);
    }
}

