/**
 * @ProjectName: 智慧评标-主观分助手
 * @Package: com.zhpb.gdyd_zhpb_zgf.controller
 * @ClassName: PromptTemplateController
 * @Description: 提示词模板控制器
 * @Author: 张志锋
 * @Date: 2025-11-03
 * @Version: 1.0
 * @Copyright: © 2025 智慧评标-主观分助手
 */
package com.zhpb.gdyd_zhpb_zgf.controller;

import com.zhpb.gdyd_zhpb_zgf.dto.PromptTemplateDto;
import com.zhpb.gdyd_zhpb_zgf.service.PromptTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提示词模板控制器
 */
@RestController
@RequestMapping("/api/v1/prompt-templates")
@Tag(name = "提示词模板", description = "提示词模板管理接口")
public class PromptTemplateController {

    @Autowired
    private PromptTemplateService promptTemplateService;

    @GetMapping
    @Operation(summary = "获取所有提示词模板", description = "获取所有提示词模板，可按类型过滤")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功")
    })
    public ResponseEntity<List<PromptTemplateDto>> getAllTemplates(
            @Parameter(description = "模板类型，可为空")
            @RequestParam(required = false) String templateType) {

        List<PromptTemplateDto> templates = promptTemplateService.getAllTemplates(templateType);
        return ResponseEntity.ok(templates);
    }

    @GetMapping("/{templateId}")
    @Operation(summary = "获取指定模板", description = "根据模板ID获取模板详情")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ResponseEntity<Map<String, Object>> getTemplateById(
            @Parameter(description = "模板ID", required = true)
            @PathVariable String templateId) {

        PromptTemplateDto template = promptTemplateService.getTemplateById(templateId);
        if (template == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "模板不存在");
            return ResponseEntity.ok(response);
        }
        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("message", "Success");
        response.put("data", template);
        return ResponseEntity.ok(response);
    }

    @PostMapping
    @Operation(summary = "创建提示词模板", description = "创建新的提示词模板")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "参数错误")
    })
    public ResponseEntity<PromptTemplateDto> createTemplate(
            @Parameter(description = "模板信息", required = true)
            @RequestBody @Validated PromptTemplateDto dto,
            @Parameter(description = "操作用户ID")
            @RequestParam(defaultValue = "admin") String userId) {

        try {
            PromptTemplateDto createdTemplate = promptTemplateService.createTemplate(dto, userId);
            return ResponseEntity.ok(createdTemplate);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{templateId}")
    @Operation(summary = "更新提示词模板", description = "更新指定模板的信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "404", description = "模板不存在"),
            @ApiResponse(responseCode = "400", description = "参数错误")
    })
    public ResponseEntity<PromptTemplateDto> updateTemplate(
            @Parameter(description = "模板ID", required = true)
            @PathVariable String templateId,
            @Parameter(description = "模板信息", required = true)
            @RequestBody @Validated PromptTemplateDto dto,
            @Parameter(description = "操作用户ID")
            @RequestParam(defaultValue = "admin") String userId) {

        try {
            PromptTemplateDto updatedTemplate = promptTemplateService.updateTemplate(templateId, dto, userId);
            return ResponseEntity.ok(updatedTemplate);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @DeleteMapping("/{templateId}")
    @Operation(summary = "删除提示词模板", description = "删除指定的提示词模板")
    @ApiResponses({
            @ApiResponse(responseCode = "204", description = "删除成功"),
            @ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ResponseEntity<Map<String, Object>> deleteTemplate(
            @Parameter(description = "模板ID", required = true)
            @PathVariable String templateId) {

        try {
            promptTemplateService.deleteTemplate(templateId);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("message", "删除成功");
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "模板不存在");
            return ResponseEntity.ok(response);
        }
    }

    @PutMapping("/{templateId}/status")
    @Operation(summary = "切换模板状态", description = "启用或禁用指定的提示词模板")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "404", description = "模板不存在")
    })
    public ResponseEntity<Map<String, Object>> toggleTemplateStatus(
            @Parameter(description = "模板ID", required = true)
            @PathVariable String templateId,
            @Parameter(description = "是否启用", required = true)
            @RequestParam Boolean isActive,
            @Parameter(description = "操作用户ID")
            @RequestParam(defaultValue = "admin") String userId) {

        try {
            promptTemplateService.toggleTemplateStatus(templateId, isActive, userId);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("message", "操作成功");
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "模板不存在");
            return ResponseEntity.ok(response);
        }
    }
}
