package com.ga.api.controller.deduction;

import com.ga.api.service.deduction.DeductionService;
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.settle.AgentDeductionResp;
import com.ga.core.vo.settle.AgentDeductionSaveReq;
import com.ga.core.vo.settle.AgentDeductionSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "공제 관리")
@RestController
@RequestMapping("/api/deductions")
@RequiredArgsConstructor
public class DeductionController {

    private final DeductionService deductionService;

    @GetMapping
    @RequirePermission(menu = "DEDUCTION", perm = "READ")
    public ApiResponse<PageResponse<AgentDeductionResp>> list(@ModelAttribute AgentDeductionSearchParam param) {
        return ApiResponse.ok(deductionService.list(param));
    }

    @GetMapping("/{deductionId}")
    @RequirePermission(menu = "DEDUCTION", perm = "READ")
    public ApiResponse<AgentDeductionResp> detail(@PathVariable Long deductionId) {
        return ApiResponse.ok(deductionService.detail(deductionId));
    }

    @PostMapping
    @RequirePermission(menu = "DEDUCTION", perm = "CREATE")
    @DataChangeLog(menu = "DEDUCTION", table = "agent_deduction")
    public ApiResponse<Long> create(@Valid @RequestBody AgentDeductionSaveReq req) {
        return ApiResponse.ok(deductionService.create(req));
    }

    @PutMapping("/{deductionId}")
    @RequirePermission(menu = "DEDUCTION", perm = "UPDATE")
    @DataChangeLog(menu = "DEDUCTION", table = "agent_deduction")
    public ApiResponse<Void> update(@PathVariable Long deductionId,
                                    @Valid @RequestBody AgentDeductionSaveReq req) {
        deductionService.update(deductionId, req);
        return ApiResponse.ok();
    }

    @PutMapping("/{deductionId}/cancel")
    @RequirePermission(menu = "DEDUCTION", perm = "UPDATE")
    @DataChangeLog(menu = "DEDUCTION", table = "agent_deduction")
    public ApiResponse<Void> cancel(@PathVariable Long deductionId) {
        deductionService.cancel(deductionId);
        return ApiResponse.ok();
    }
}
