package com.nanri.aiimage.modules.dedupe.controller;

import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.dedupe.model.dto.DedupeTotalDataCreateRequest;
import com.nanri.aiimage.modules.dedupe.model.dto.DedupeTotalDataUpdateRequest;
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataImportProgressVo;
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataImportStartVo;
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataImportVo;
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataItemVo;
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataPageVo;
import com.nanri.aiimage.modules.dedupe.service.DedupeTotalDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/admin/dedupe-total-data")
@Tag(name = "数据去重总数据", description = "维护数据去重模块的总数据列表，支持增删改查。")
public class DedupeTotalDataController {

    private final DedupeTotalDataService dedupeTotalDataService;

    @GetMapping
    @Operation(summary = "分页查询总数据", description = "分页查询数据去重总数据，支持按值模糊搜索。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = DedupeTotalDataPageVo.class)))
    })
    public ApiResponse<DedupeTotalDataPageVo> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "15") Long pageSize,
            @Parameter(description = "模糊搜索关键字") @RequestParam(required = false) String keyword) {
        return ApiResponse.success(dedupeTotalDataService.page(page, pageSize, keyword));
    }

    @PostMapping
    @Operation(summary = "新增总数据", description = "新增一条数据去重总数据。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功", content = @Content(schema = @Schema(implementation = DedupeTotalDataItemVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数不合法或数据重复")
    })
    public ApiResponse<DedupeTotalDataItemVo> create(@Valid @RequestBody DedupeTotalDataCreateRequest request) {
        return ApiResponse.success("创建成功", dedupeTotalDataService.create(request));
    }

    @PostMapping("/import")
    @Operation(summary = "导入总数据", description = "上传 xlsx 文件，读取 ASIN 列并批量导入数据去重总数据。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "启动导入成功", content = @Content(schema = @Schema(implementation = DedupeTotalDataImportStartVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "文件不合法或缺少 ASIN 列")
    })
    public ApiResponse<DedupeTotalDataImportStartVo> importExcel(
            @Parameter(description = "xlsx 文件", required = true) @RequestParam("file") MultipartFile file) {
        return ApiResponse.success("开始导入", dedupeTotalDataService.startImport(file));
    }

    @GetMapping("/import/{importId}")
    @Operation(summary = "查询导入进度", description = "根据导入任务 ID 查询当前进度。")
    public ApiResponse<DedupeTotalDataImportProgressVo> importProgress(@PathVariable String importId) {
        return ApiResponse.success(dedupeTotalDataService.getImportProgress(importId));
    }

    @PostMapping("/delete-import")
    @Operation(summary = "删除导入总数据", description = "上传 xlsx 文件，读取 ASIN 列并批量删除已存在的数据去重总数据。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "启动删除成功", content = @Content(schema = @Schema(implementation = DedupeTotalDataImportStartVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "文件不合法或缺少 ASIN 列")
    })
    public ApiResponse<DedupeTotalDataImportStartVo> deleteImportExcel(
            @Parameter(description = "xlsx 文件", required = true) @RequestParam("file") MultipartFile file) {
        return ApiResponse.success("开始删除", dedupeTotalDataService.startDeleteImport(file));
    }

    @GetMapping("/delete-import/{importId}")
    @Operation(summary = "查询删除导入进度", description = "根据删除导入任务 ID 查询当前进度。")
    public ApiResponse<DedupeTotalDataImportProgressVo> deleteImportProgress(@PathVariable String importId) {
        return ApiResponse.success(dedupeTotalDataService.getDeleteImportProgress(importId));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新总数据", description = "按 ID 更新一条数据去重总数据。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功", content = @Content(schema = @Schema(implementation = DedupeTotalDataItemVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数不合法或数据重复"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "数据不存在")
    })
    public ApiResponse<DedupeTotalDataItemVo> update(
            @Parameter(description = "主键ID", required = true) @PathVariable Long id,
            @Valid @RequestBody DedupeTotalDataUpdateRequest request) {
        return ApiResponse.success("更新成功", dedupeTotalDataService.update(id, request));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除总数据", description = "按 ID 删除一条数据去重总数据。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "数据不存在")
    })
    public ApiResponse<Void> delete(@Parameter(description = "主键ID", required = true) @PathVariable Long id) {
        dedupeTotalDataService.delete(id);
        return ApiResponse.success("删除成功", null);
    }
}
