package com.zioinfo.hrm.performance;

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.common.AuthSupport;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hrm/performance")
@RequiredArgsConstructor
public class PerformanceController {

    private final PerformanceService performanceService;

    @GetMapping("/reviews")
    public ApiResponse<Map<String, Object>> listReviews(
            @RequestParam(defaultValue = "0") int year,
            @RequestParam(required = false) Long empId,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(performanceService.listReviews(year, empId, status, page, size));
    }

    @GetMapping("/reviews/{id}")
    public ApiResponse<Map<String, Object>> getReview(@PathVariable Long id) {
        return ApiResponse.ok(performanceService.getReview(id));
    }

    @PostMapping("/reviews")
    public ApiResponse<Void> createReview(@RequestBody Map<String, Object> review, Authentication auth) {
        performanceService.createReview(review, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

    @PutMapping("/reviews/{id}")
    public ApiResponse<Void> updateReview(@PathVariable Long id, @RequestBody Map<String, Object> review) {
        performanceService.updateReview(id, review);
        return ApiResponse.ok(null);
    }

    @PostMapping("/reviews/{id}/submit")
    public ApiResponse<Void> submit(@PathVariable Long id) {
        performanceService.submitReview(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/reviews/{id}/approve")
    public ApiResponse<Void> approve(@PathVariable Long id, Authentication auth) {
        AuthSupport.requireManager(auth);
        performanceService.approveReview(id);
        return ApiResponse.ok(null);
    }

    @GetMapping("/goals/{empId}")
    public ApiResponse<List<Map<String, Object>>> getGoals(
            @PathVariable Long empId, @RequestParam(defaultValue = "0") int year) {
        return ApiResponse.ok(performanceService.getGoals(empId, year));
    }

    @PostMapping("/goals/{empId}")
    public ApiResponse<Void> createGoal(@PathVariable Long empId,
                                         @RequestBody Map<String, Object> goal, Authentication auth) {
        performanceService.createGoal(empId, goal, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

    @PutMapping("/goals/{id}")
    public ApiResponse<Void> updateGoal(@PathVariable Long id, @RequestBody Map<String, Object> goal) {
        performanceService.updateGoal(id, goal);
        return ApiResponse.ok(null);
    }

    @GetMapping("/competencies/{empId}")
    public ApiResponse<List<Map<String, Object>>> getCompetencies(
            @PathVariable Long empId, @RequestParam(defaultValue = "0") int year) {
        return ApiResponse.ok(performanceService.getCompetencies(empId, year));
    }

    @PostMapping("/competencies/{empId}")
    public ApiResponse<Void> addScore(@PathVariable Long empId,
                                       @RequestBody Map<String, Object> score, Authentication auth) {
        performanceService.addCompetencyScore(empId, score, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }
}
