package com.jeesharp.business.modules.setting.controller;

import cn.hutool.core.util.StrUtil;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.setting.application.FeedbackApplicationService;
import com.jeesharp.business.modules.setting.dto.FeedbackHandleDTO;
import com.jeesharp.business.modules.setting.entity.Feedback;
import com.jeesharp.business.modules.setting.vo.FeedbackQueryVO;
import com.jeesharp.business.modules.setting.vo.FeedbackVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.web.controller.BaseController;
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 jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 意见反馈管理 Controller（后台管理）
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/feedback/v1")
@Tag(name = "意见反馈管理", description = "意见反馈管理相关接口，包括反馈的查询、处理、关闭等功能")
@AllArgsConstructor
public class FeedbackController extends BaseController {

    private final FeedbackApplicationService feedbackApplicationService;

    // ==================== 基础 CRUD ====================

    /**
     * 获取反馈详情
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "1、获取反馈详情", description = "根据反馈ID获取反馈的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "403", description = "无权限访问")
    })
    @PreAuthorize("@ss.hasPermission('setting:feedback:info')")
    @GetMapping(value = "info")
    public AjaxResult<FeedbackVO> info(
            @Parameter(description = "反馈ID", required = true)
            @RequestParam("id") String id) {
        return AjaxResult.okData(feedbackApplicationService.getFeedback(id));
    }

    /**
     * 获取反馈列表（分页）
     */
    @ApiOperationSupport(order = 2)
    @Operation(summary = "2、获取反馈列表", description = "分页查询反馈列表，支持多条件筛选")
    @PreAuthorize("@ss.hasPermission('setting:feedback:list')")
    @PostMapping(value = "list")
    public AjaxResult<Page<FeedbackVO>> list(@RequestBody FeedbackQueryVO query) {
        Page<Feedback> page = new Page<>(query);
        return AjaxResult.okData(feedbackApplicationService.findPage(page, query));
    }

    /**
     * 删除反馈
     */
    @ApiOperationSupport(order = 3)
    @Operation(summary = "3、删除反馈", description = "批量删除反馈，多个ID用逗号分隔")
    @PreAuthorize("@ss.hasPermission('setting:feedback:delete')")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(
            @Parameter(description = "反馈ID，多个用逗号分隔", required = true)
            @RequestParam("ids") String ids) {
        feedbackApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    // ==================== 反馈处理 ====================

    /**
     * 处理反馈（标记为处理中）
     */
    @ApiOperationSupport(order = 4)
    @Operation(summary = "4、处理反馈", description = "标记反馈为处理中状态，并记录处理人信息")
    @PreAuthorize("@ss.hasPermission('setting:feedback:handle')")
    @PostMapping(value = "handle")
    public AjaxResult<Void> handle(@RequestBody @Valid FeedbackHandleDTO dto) {
        feedbackApplicationService.handleFeedback(dto.getId(), dto.getHandleResult());
        return AjaxResult.ok("处理成功");
    }

    /**
     * 完成反馈处理（标记为已处理）
     */
    @ApiOperationSupport(order = 5)
    @Operation(summary = "5、完成反馈处理", description = "标记反馈为已处理状态，并记录处理结果")
    @PreAuthorize("@ss.hasPermission('setting:feedback:complete')")
    @PostMapping(value = "complete")
    public AjaxResult<Void> complete(@RequestBody @Valid FeedbackHandleDTO dto) {
        feedbackApplicationService.completeFeedback(dto.getId(), dto.getHandleResult());
        return AjaxResult.ok("处理完成");
    }

    /**
     * 关闭反馈
     */
    @ApiOperationSupport(order = 6)
    @Operation(summary = "6、关闭反馈", description = "关闭反馈，标记为已关闭状态")
    @PreAuthorize("@ss.hasPermission('setting:feedback:close')")
    @GetMapping(value = "close")
    public AjaxResult<Void> close(
            @Parameter(description = "反馈ID", required = true)
            @RequestParam("id") String id) {
        feedbackApplicationService.closeFeedback(id);
        return AjaxResult.ok("关闭成功");
    }
}
