package com.ga.api.controller.income;

import com.ga.api.service.income.AgentAnnualIncomeService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.income.AgentAnnualIncomeResp;
import com.ga.core.vo.income.AgentAnnualIncomeSearchParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "연간 사업소득")
@RestController
@RequestMapping("/api/agent-annual-incomes")
@RequiredArgsConstructor
public class AgentAnnualIncomeController {

    private final AgentAnnualIncomeService service;

    @GetMapping
    @Operation(summary = "연간소득 목록 조회")
    @RequirePermission(menu = "ANNUAL_INCOME", perm = "READ")
    public ApiResponse<PageResponse<AgentAnnualIncomeResp>> list(
            @ModelAttribute AgentAnnualIncomeSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{agentId}/{settleYear}")
    @Operation(summary = "연간소득 상세 조회")
    @RequirePermission(menu = "ANNUAL_INCOME", perm = "READ")
    public ApiResponse<AgentAnnualIncomeResp> detail(
            @PathVariable Long agentId,
            @PathVariable int settleYear) {
        return ApiResponse.ok(service.detail(agentId, settleYear));
    }

    @PostMapping("/regenerate")
    @Operation(summary = "연도 전체 재집계")
    @RequirePermission(menu = "ANNUAL_INCOME", perm = "EXECUTE")
    @DataChangeLog(menu = "ANNUAL_INCOME", table = "agent_annual_income")
    public ApiResponse<Integer> regenerate(@RequestParam int year) {
        return ApiResponse.ok(service.regenerate(year));
    }
}
