package com.dne.ems.controller;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dne.ems.dto.RejectFeedbackRequest;
import com.dne.ems.model.Feedback;
import com.dne.ems.service.SupervisorService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 主管控制器，处理反馈审核相关功能
 *
 * <p>主要职责：
 * <ul>
 *   <li>获取待审核反馈列表</li>
 *   <li>审核通过/拒绝反馈</li>
 * </ul>
 *
 * @author DNE开发团队
 * @version 1.0
 * @since 2024
 */
@RestController
@RequestMapping("/api/supervisor")
@RequiredArgsConstructor
@Tag(name = "Supervisor", description = "Endpoints for supervisor to review and manage feedback")
public class SupervisorController {

    private final SupervisorService supervisorService;

    /**
     * 获取待审核反馈列表
     *
     * @return 待审核反馈列表
     * @throws AccessDeniedException 如果用户无权限
     */
    @GetMapping("/reviews")
    @PreAuthorize("hasAnyRole('SUPERVISOR', 'ADMIN')")
    @Operation(summary = "Get feedback list pending for supervisor review")
    public ResponseEntity<List<Feedback>> getFeedbackForReview() {
        List<Feedback> feedbackList = supervisorService.getFeedbackForReview();
        return ResponseEntity.ok(feedbackList);
    }

    /**
     * 审核通过反馈
     *
     * @param feedbackId 反馈ID
     * @return 空响应
     * @throws ResourceNotFoundException 如果反馈不存在
     * @throws InvalidOperationException 如果反馈已审核
     */
    @PostMapping("/reviews/{feedbackId}/approve")
    @PreAuthorize("hasAnyRole('SUPERVISOR', 'ADMIN')")
    @Operation(summary = "Approve a feedback")
    public ResponseEntity<Void> approveFeedback(@PathVariable Long feedbackId) {
        supervisorService.approveFeedback(feedbackId);
        return ResponseEntity.ok().build();
    }

    /**
     * 审核拒绝反馈
     *
     * @param feedbackId 反馈ID
     * @return 空响应
     * @throws ResourceNotFoundException 如果反馈不存在
     * @throws InvalidOperationException 如果反馈已审核
     */
    @PostMapping("/reviews/{feedbackId}/reject")
    @PreAuthorize("hasAnyRole('SUPERVISOR', 'ADMIN')")
    @Operation(summary = "Reject a feedback")
    public ResponseEntity<Void> rejectFeedback(
            @PathVariable Long feedbackId,
            @RequestBody RejectFeedbackRequest request) {
        supervisorService.rejectFeedback(feedbackId, request);
        return ResponseEntity.ok().build();
    }
} 