package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.dto.LifeFeedbackDto;
import shop.alien.entity.store.dto.UserReplyDto;
import shop.alien.entity.store.dto.*;
import shop.alien.entity.store.vo.LifeFeedbackDetailVo;
import shop.alien.entity.store.vo.LifeFeedbackListVo;
import shop.alien.entity.store.vo.LifeFeedbackVo;
import shop.alien.store.service.LifeFeedbackService;

/**
 * 意见反馈 Controller
 */
@Api(tags = {"意见反馈模块"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/feedback")
@RequiredArgsConstructor
public class LifeFeedbackController {

    private final LifeFeedbackService lifeFeedbackService;

    @ApiOperation(value = "提交反馈", httpMethod = "POST")
    @PostMapping("/submit")
    public R<String> submitFeedback(@RequestBody LifeFeedbackDto dto) {
        log.info("LifeFeedbackController.submitFeedback, dto={}", dto);
        return lifeFeedbackService.submitFeedback(dto);
    }

    @ApiOperation(value = "查询用户历史反馈列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "feedbackSource", value = "反馈来源:0-用户端,1-商家端", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/list")
    public R<IPage<LifeFeedbackVo>> getFeedbackList(
            @RequestParam("userId") Integer userId,
            @RequestParam("feedbackSource") Integer feedbackSource,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        log.info("LifeFeedbackController.getFeedbackList, userId={}, feedbackSource={}, page={}, size={}", 
                userId, feedbackSource, page, size);
        IPage<LifeFeedbackVo> result = lifeFeedbackService.getFeedbackList(userId, feedbackSource, page, size);
        return R.data(result);
    }

    @ApiOperation(value = "查询反馈详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedbackId", value = "反馈ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<LifeFeedbackVo> getFeedbackDetail(@RequestParam("feedbackId") Integer feedbackId) {
        log.info("LifeFeedbackController.getFeedbackDetail, feedbackId={}", feedbackId);
        return lifeFeedbackService.getFeedbackDetail(feedbackId);
    }

    @ApiOperation(value = "用户回复", httpMethod = "POST")
    @PostMapping("/userReply")
    public R<String> userReply(@RequestBody UserReplyDto dto) {
        log.info("LifeFeedbackController.userReply, dto={}", dto);
        return lifeFeedbackService.userReply(dto);
    }

    // ==================== 中台接口 ====================

    @ApiOperation(value = "中台-查询意见反馈列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedbackType", value = "反馈类型:0-优化建议,1-问题", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "handleStatus", value = "处理状态:0-处理中,1-已解决", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "feedbackSource", value = "反馈来源:0-用户端,1-商家端", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "feedbackWay", value = "反馈方式:0-用户反馈,1-AI识别", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/platform/list")
    public R<IPage<LifeFeedbackListVo>> getWebFeedbackList(LifeFeedbackQueryDto queryDto) {
        log.info("LifeFeedbackController.getWebFeedbackList, queryDto={}", queryDto);
        return lifeFeedbackService.getWebFeedbackList(queryDto);
    }

    @ApiOperation(value = "中台-查询反馈详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedbackId", value = "反馈ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/platform/detail")
    public R<LifeFeedbackDetailVo> getWebFeedbackDetail(@RequestParam("feedbackId") Integer feedbackId) {
        log.info("LifeFeedbackController.getWebFeedbackDetail, feedbackId={}", feedbackId);
        return lifeFeedbackService.getWebFeedbackDetail(feedbackId);
    }

    @ApiOperation(value = "中台-回复用户", httpMethod = "POST")
    @PostMapping("/platform/reply")
    public R<String> webReplyUser(@RequestBody LifeFeedbackReplyWebDto replyDto) {
        log.info("LifeFeedbackController.webReplyUser, replyDto={}", replyDto);
        return lifeFeedbackService.webReplyUser(replyDto);
    }

    @ApiOperation(value = "中台-更新处理状态", httpMethod = "POST")
    @PostMapping("/platform/updateStatus")
    public R<String> updateWebFeedbackStatus(@RequestBody LifeFeedbackStatusDto statusDto) {
        log.info("LifeFeedbackController.updateWebFeedbackStatus, statusDto={}", statusDto);
        return lifeFeedbackService.updateWebFeedbackStatus(statusDto);
    }
}

