package com.zioinfo.hrm.payroll;

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.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hrm/payroll")
@RequiredArgsConstructor
public class PayrollController {

    private final PayrollService payrollService;

    @GetMapping
    public ApiResponse<Map<String, Object>> list(
            @RequestParam(defaultValue = "#{T(java.time.LocalDate).now().year}") int year,
            @RequestParam(defaultValue = "#{T(java.time.LocalDate).now().monthValue}") int month,
            @RequestParam(required = false) Long deptId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        int y = year == 0 ? LocalDate.now().getYear() : year;
        int m = month == 0 ? LocalDate.now().getMonthValue() : month;
        return ApiResponse.ok(payrollService.list(y, m, deptId, page, size));
    }

    @GetMapping("/payslip/{empId}")
    public ApiResponse<Map<String, Object>> getPayslip(
            @PathVariable Long empId,
            @RequestParam int year,
            @RequestParam int month) {
        return ApiResponse.ok(payrollService.getPayslip(empId, year, month));
    }

    @GetMapping("/payslips/{empId}")
    public ApiResponse<List<Map<String, Object>>> getPayslipsByEmp(
            @PathVariable Long empId,
            @RequestParam(defaultValue = "0") int year) {
        int y = year == 0 ? LocalDate.now().getYear() : year;
        return ApiResponse.ok(payrollService.getPayslipsByEmp(empId, y));
    }

    @PostMapping("/process")
    public ApiResponse<Void> process(@RequestParam int year, @RequestParam int month, Authentication auth) {
        AuthSupport.requireManager(auth);
        payrollService.processPayroll(year, month, AuthSupport.actor(auth));
        return ApiResponse.ok(null);
    }

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

    @GetMapping("/salary/{empId}")
    public ApiResponse<Map<String, Object>> getSalary(@PathVariable Long empId) {
        return ApiResponse.ok(payrollService.getSalary(empId));
    }

    @PutMapping("/salary/{empId}")
    public ApiResponse<Void> upsertSalary(@PathVariable Long empId, @RequestBody Map<String, Object> salary,
                                            Authentication auth) {
        AuthSupport.requireManager(auth);
        payrollService.upsertSalary(empId, salary);
        return ApiResponse.ok(null);
    }

    @GetMapping("/yearly/{empId}")
    public ApiResponse<Map<String, Object>> yearlySummary(@PathVariable Long empId,
                                                            @RequestParam(defaultValue = "0") int year) {
        int y = year == 0 ? LocalDate.now().getYear() : year;
        return ApiResponse.ok(payrollService.getYearlySummary(empId, y));
    }

    @GetMapping("/items")
    public ApiResponse<List<Map<String, Object>>> salaryItems() {
        return ApiResponse.ok(payrollService.getSalaryItems());
    }
}
