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

import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.convert.model.dto.ConvertTemplateImportRequest;
import com.nanri.aiimage.modules.convert.model.vo.ConvertTemplateVo;
import com.nanri.aiimage.modules.convert.service.ConvertTemplateService;
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 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/convert/templates")
@Tag(name = "格式转换模板", description = "用于维护和查询格式转换模板，支持查询、导入、设默认、删除。")
public class ConvertTemplateController {

    private final ConvertTemplateService convertTemplateService;

    @GetMapping
    @Operation(summary = "查询启用模板", description = "返回当前启用的格式转换模板列表，供前端‘格式转换’模块展示。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功，返回模板列表")
    })
    public ApiResponse<List<ConvertTemplateVo>> listTemplates() {
        return ApiResponse.success(convertTemplateService.listEnabledTemplates());
    }

    @PostMapping("/import")
    @Operation(summary = "导入模板", description = "导入自定义 txt 模板，成功后返回新模板并可立即刷新模板列表。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "导入成功，返回新模板信息", content = @Content(schema = @Schema(implementation = ConvertTemplateVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "模板内容不合法或必填字段缺失")
    })
    public ApiResponse<ConvertTemplateVo> importTemplate(@RequestBody ConvertTemplateImportRequest request) {
        return ApiResponse.success(convertTemplateService.importTemplate(request));
    }

    @PostMapping("/{templateCode}/default")
    @Operation(summary = "设为默认模板", description = "将指定模板设为默认模板。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "设置成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> setDefault(@Parameter(description = "模板编码", required = true) @PathVariable String templateCode) {
        convertTemplateService.setDefaultTemplate(templateCode);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{templateCode}")
    @Operation(summary = "删除模板", description = "删除指定模板。内置模板不允许删除。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "内置模板不允许删除"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ApiResponse<Void> delete(@Parameter(description = "模板编码", required = true) @PathVariable String templateCode) {
        convertTemplateService.deleteTemplate(templateCode);
        return ApiResponse.success(null);
    }
}
