package top.tqx.demo_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.tqx.demo_1.dto.FeedbackCreateRequest;
import top.tqx.demo_1.dto.FeedbackListResponse;
import top.tqx.demo_1.dto.FeedbackResponse;
import top.tqx.demo_1.dto.FeedbackStatsResponse;
import top.tqx.demo_1.service.FeedbackService;

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

@Tag(name = "反馈管理", description = "用户反馈相关接口")
@RestController
@RequestMapping("/feedback")
@Slf4j
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @Operation(summary = "提交反馈", description = "用户提交对AI回答的反馈")
    @PostMapping
    public Map<String, Object> submitFeedback(@RequestBody FeedbackCreateRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("提交反馈: {}", request);
            FeedbackResponse response = feedbackService.submitFeedback(request);
            result.put("success", response.getSuccess());
            if (response.getFeedbackId() != null) {
                result.put("feedbackId", response.getFeedbackId());
            }
            if (response.getFaqSuggested() != null) {
                result.put("faqSuggested", response.getFaqSuggested());
            }
            if (response.getSuggestionId() != null) {
                result.put("suggestionId", response.getSuggestionId());
            }
            result.put("message", "反馈提交成功");
        } catch (Exception e) {
            log.error("提交反馈失败", e);
            result.put("success", false);
            result.put("message", "反馈提交失败: " + e.getMessage());
        }
        return result;
    }

    @Operation(summary = "获取反馈列表", description = "分页获取所有反馈记录")
    @GetMapping("/list")
    public Map<String, Object> getFeedbackList(
            @Parameter(description = "页码，默认1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小，默认20") @RequestParam(defaultValue = "20") Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("获取反馈列表: page={}, pageSize={}", page, pageSize);
            FeedbackListResponse response = feedbackService.getFeedbackList(page, pageSize);
            result.put("success", response.getSuccess());
            result.put("data", response.getFeedbacks());
            result.put("total", response.getTotal());
            result.put("page", page);
            result.put("pageSize", pageSize);
        } catch (Exception e) {
            log.error("获取反馈列表失败", e);
            result.put("success", false);
            result.put("message", "获取反馈列表失败: " + e.getMessage());
        }
        return result;
    }

    @Operation(summary = "获取反馈统计", description = "获取反馈统计信息，包括总数量、好评差评数量、满意度等")
    @GetMapping("/stats")
    public Map<String, Object> getFeedbackStats() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("获取反馈统计");
            FeedbackStatsResponse response = feedbackService.getFeedbackStats();
            result.put("success", true);
            result.put("data", response);
        } catch (Exception e) {
            log.error("获取反馈统计失败", e);
            result.put("success", false);
            result.put("message", "获取反馈统计失败: " + e.getMessage());
        }
        return result;
    }

    @Operation(summary = "获取差评案例", description = "获取所有差评反馈记录")
    @GetMapping("/bad-cases")
    public Map<String, Object> getBadCases() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("获取差评案例");
            FeedbackListResponse response = feedbackService.getBadCases();
            result.put("success", response.getSuccess());
            result.put("data", response.getFeedbacks());
            result.put("total", response.getTotal());
        } catch (Exception e) {
            log.error("获取差评案例失败", e);
            result.put("success", false);
            result.put("message", "获取差评案例失败: " + e.getMessage());
        }
        return result;
    }
}
