package com.ga.api.controller.commission;

import com.ga.api.service.commission.CommissionMasterService;
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.commission.CommissionMasterResp;
import com.ga.core.vo.commission.CommissionMasterSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "수수료 마스터")
@RestController
@RequestMapping("/api/commissions")
@RequiredArgsConstructor
public class CommissionMasterController {

    private final CommissionMasterService service;

    @GetMapping
    @RequirePermission(menu = "COMM_MASTER", perm = "READ")
    public ApiResponse<PageResponse<CommissionMasterResp>> list(@ModelAttribute CommissionMasterSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{masterId}")
    @RequirePermission(menu = "COMM_MASTER", perm = "READ")
    public ApiResponse<CommissionMasterResp> detail(@PathVariable Long masterId) {
        return ApiResponse.ok(service.detail(masterId));
    }

    @GetMapping("/by-agent")
    @RequirePermission(menu = "COMM_MASTER", perm = "READ")
    public ApiResponse<List<CommissionMasterResp>> byAgent(@RequestParam Long agentId,
                                                           @RequestParam String settleMonth) {
        return ApiResponse.ok(service.listByAgentAndMonth(agentId, settleMonth));
    }

    @GetMapping("/aggregated")
    @RequirePermission(menu = "COMM_MASTER", perm = "READ")
    public ApiResponse<List<CommissionMasterResp>> aggregated(@RequestParam String settleMonth) {
        return ApiResponse.ok(service.aggregatedByMonth(settleMonth));
    }

    @PutMapping("/{masterId}/status")
    @RequirePermission(menu = "COMM_MASTER", perm = "UPDATE")
    @DataChangeLog(menu = "COMM_MASTER", table = "commission_master")
    public ApiResponse<Void> updateStatus(@PathVariable Long masterId,
                                          @RequestBody Map<String, String> body) {
        service.updateStatus(masterId, body.get("status"));
        return ApiResponse.ok();
    }

    @DeleteMapping("/{masterId}")
    @RequirePermission(menu = "COMM_MASTER", perm = "DELETE")
    @DataChangeLog(menu = "COMM_MASTER", table = "commission_master")
    public ApiResponse<Void> delete(@PathVariable Long masterId) {
        service.delete(masterId);
        return ApiResponse.ok();
    }
}
