package com.dne.ems.controller;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dne.ems.dto.UserFeedbackSummaryDTO;
import com.dne.ems.security.CustomUserDetails;
import com.dne.ems.service.UserFeedbackService;

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

/**
 * 用户个人资料控制器，处理认证用户相关数据
 *
 * <p>主要功能：
 * <ul>
 *   <li>获取用户提交的反馈历史</li>
 *   <li>管理个人资料相关数据</li>
 * </ul>
 *
 * @author DNE开发团队
 * @version 1.2
 * @since 2025-06-16
 */
@RestController
@RequestMapping("/api/me")
@RequiredArgsConstructor
@Tag(name = "User Profile", description = "Endpoints related to the authenticated user's own data")
@SecurityRequirement(name = "bearerAuth")
public class ProfileController {

    private final UserFeedbackService userFeedbackService;

    /**
     * 获取当前用户的反馈历史记录
     *
     * <p>实现细节：
     * <ol>
     *   <li>从认证信息获取用户ID</li>
     *   <li>查询该用户提交的所有反馈</li>
     *   <li>返回分页结果</li>
     * </ol>
     *
     * @param userDetails 认证用户详情
     * @param pageable 分页参数
     * @return 用户反馈历史的分页列表
     * @throws AuthenticationException 如果用户未认证
     */
    @Operation(summary = "Get current user's feedback history", description = "Retrieves a paginated list of feedback submitted by the currently authenticated user.")
    @GetMapping("/feedback")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<List<UserFeedbackSummaryDTO>> getMyFeedbackHistory(
            @AuthenticationPrincipal CustomUserDetails userDetails,
            Pageable pageable) {
        Page<UserFeedbackSummaryDTO> historyPage = userFeedbackService.getFeedbackHistoryByUserId(userDetails.getId(), pageable);
        return ResponseEntity.ok(historyPage.getContent());
    }
} 