package com.yimeng.okr.controller;

import com.yimeng.okr.dto.ApiResult;
import com.yimeng.okr.annotation.OperationLogAnnotation;
import com.yimeng.okr.dto.ScoringDto;
import com.yimeng.okr.entity.PerformanceFeedback;
import com.yimeng.okr.security.SecurityUtils;
import com.yimeng.okr.service.FeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/feedback")
@RequiredArgsConstructor
public class FeedbackController {

    private final FeedbackService feedbackService;

    @PostMapping("/interview")
    @OperationLogAnnotation(action = "SAVE_INTERVIEW", entityType = "performance_feedback", sensitive = true)
    public ApiResult<PerformanceFeedback> saveInterview(@RequestBody Map<String, Object> body) {
        Long periodId = Long.valueOf(body.get("periodId").toString());
        Long userId = Long.valueOf(body.get("userId").toString());
        Long superiorId = SecurityUtils.getCurrentUserId();
        String notes = body.getOrDefault("interviewNotes", "").toString();
        String highlights = body.getOrDefault("highlights", "").toString();
        String issues = body.getOrDefault("issues", "").toString();
        String improvements = body.getOrDefault("improvements", "").toString();
        return ApiResult.success(
                feedbackService.saveInterviewNotes(periodId, userId, superiorId, notes, highlights, issues, improvements));
    }

    @PostMapping("/reply")
    @OperationLogAnnotation(action = "SUBMIT_FEEDBACK_REPLY", entityType = "performance_feedback", sensitive = true)
    public ApiResult<PerformanceFeedback> submitReply(@RequestBody Map<String, String> body) {
        Long periodId = Long.valueOf(body.get("periodId"));
        Long userId = SecurityUtils.getCurrentUserId();
        String feedback = body.get("employeeFeedback");
        return ApiResult.success(feedbackService.submitEmployeeFeedback(periodId, userId, feedback));
    }

    @PostMapping("/close")
    @OperationLogAnnotation(action = "CLOSE_FEEDBACK", entityType = "performance_feedback", sensitive = true)
    public ApiResult<PerformanceFeedback> closeFeedback(@RequestBody Map<String, Object> body) {
        Long periodId = Long.valueOf(body.get("periodId").toString());
        Long userId = Long.valueOf(body.get("userId").toString());
        return ApiResult.success(feedbackService.closeFeedback(periodId, userId, SecurityUtils.getCurrentUserId()));
    }

    @GetMapping("/page")
    @OperationLogAnnotation(action = "VIEW_FEEDBACK_PAGE", entityType = "performance_feedback", sensitive = true)
    public ApiResult<ScoringDto> getFeedbackPage(@RequestParam Long periodId,
                                                  @RequestParam Long userId) {
        return ApiResult.success(feedbackService.getFeedbackPage(periodId, userId, SecurityUtils.getCurrentUserId()));
    }
}
