package com.luojia_channel.modules.feedback.controller;

import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.page.PageRequest;
import com.luojia_channel.common.domain.page.PageResponse;
import com.luojia_channel.common.exception.UserException;
import com.luojia_channel.common.utils.UserContext;
import com.luojia_channel.modules.feedback.dto.FeedbackTicketBasicInfoDTO;
import com.luojia_channel.modules.feedback.dto.FeedbackTicketPageQueryDTO;
import com.luojia_channel.modules.feedback.entity.FeedbackTicket;
import com.luojia_channel.modules.feedback.service.FeedbackTicketService;
import io.swagger.v3.oas.annotations.Operation;
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 lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feedback")
@Tag(name = "反馈工单模块", description = "反馈工单相关接口")
@RequiredArgsConstructor
public class FeedbackTicketController {

    private final FeedbackTicketService feedbackTicketService;

    @PostMapping
    @Operation(summary = "创建反馈工单", description = "用户创建反馈工单")
    public Result<Long> createFeedbackTicket(@RequestBody FeedbackTicket feedbackTicket) {
        Long id = feedbackTicketService.createFeedbackTicket(feedbackTicket);
        return Result.success(id);
    }

    @PutMapping
    @Operation(summary = "编辑反馈工单", description = "编辑已有的反馈工单")
    public Result<Void> updateFeedbackTicket(@RequestBody FeedbackTicket feedbackTicket) {
        feedbackTicketService.updateFeedbackTicket(feedbackTicket);
        return Result.success();
    }

    @GetMapping("/of/me")
    @Operation(summary = "个人反馈工单查询", description = "查询当前用户的反馈工单")
    public Result<List<FeedbackTicket>> getPersonalFeedbackTickets() {
        Long userId = UserContext.getUserId();
        if(userId == null){
            throw new UserException("用户未登录");
        }
        List<FeedbackTicket> tickets = feedbackTicketService.getPersonalFeedbackTickets(userId);
        return Result.success(tickets);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询反馈工单", description = "分页查询反馈工单列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，请稍后重试")
    })
    public Result<PageResponse<FeedbackTicketBasicInfoDTO>> pageFeedbackTicket(FeedbackTicketPageQueryDTO feedbackTicketPageQueryDTO) {
        PageResponse<FeedbackTicketBasicInfoDTO> pageResponse = feedbackTicketService.pageFeedbackTicket(feedbackTicketPageQueryDTO);
        return Result.success(pageResponse);
    }
}
