package com.ga.api.controller.chargeback;

import com.ga.api.service.chargeback.ChargebackCalculator;
import com.ga.api.service.chargeback.ChargebackGradeService;
import com.ga.api.service.chargeback.ChargebackResult;
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.chargeback.ChargebackGradeResp;
import com.ga.core.vo.chargeback.ChargebackGradeSaveReq;
import com.ga.core.vo.chargeback.ChargebackGradeSearchParam;
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/chargeback-grades")
@RequiredArgsConstructor
public class ChargebackGradeController {

    private final ChargebackGradeService gradeService;
    private final ChargebackCalculator calculator;

    @GetMapping
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "READ")
    public ApiResponse<PageResponse<ChargebackGradeResp>> list(ChargebackGradeSearchParam param) {
        return ApiResponse.ok(gradeService.list(param));
    }

    @GetMapping("/{gradeId}")
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "READ")
    public ApiResponse<ChargebackGradeResp> detail(@PathVariable Long gradeId) {
        return ApiResponse.ok(gradeService.detail(gradeId));
    }

    @PostMapping
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "CREATE")
    @DataChangeLog(menu = "CHARGEBACK_GRADE", table = "chargeback_grade")
    public ApiResponse<Void> create(@Valid @RequestBody ChargebackGradeSaveReq req) {
        gradeService.create(req);
        return ApiResponse.ok();
    }

    @PutMapping("/{gradeId}")
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "UPDATE")
    @DataChangeLog(menu = "CHARGEBACK_GRADE", table = "chargeback_grade")
    public ApiResponse<Void> update(@PathVariable Long gradeId,
                                    @Valid @RequestBody ChargebackGradeSaveReq req) {
        gradeService.update(gradeId, req);
        return ApiResponse.ok();
    }

    @PutMapping("/{gradeId}/deactivate")
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "UPDATE")
    @DataChangeLog(menu = "CHARGEBACK_GRADE", table = "chargeback_grade")
    public ApiResponse<Void> deactivate(@PathVariable Long gradeId) {
        gradeService.deactivate(gradeId);
        return ApiResponse.ok();
    }

    @PostMapping("/calculate")
    @RequirePermission(menu = "CHARGEBACK_GRADE", perm = "READ")
    public ApiResponse<ChargebackResult> calculate(@Valid @RequestBody ChargebackCalculateReq req) {
        ChargebackResult result = calculator.calculate(
                req.getContractDate(),
                req.getTerminationDate(),
                req.getOriginalCommission(),
                req.getProductCategory()
        );
        return ApiResponse.ok(result);
    }
}
