package com.ga.api.controller.settle;

import com.ga.api.service.settle.PaymentService;
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.PaymentResp;
import com.ga.core.vo.settle.SettleSearchParam;
import com.ga.core.vo.settle.TransferFileResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "지급 관리")
@Validated
@RestController
@RequestMapping("/api/payments")
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService paymentService;

    @GetMapping
    @RequirePermission(menu = "PAYMENT", perm = "READ")
    public ApiResponse<PageResponse<PaymentResp>> list(@ModelAttribute SettleSearchParam param) {
        return ApiResponse.ok(paymentService.list(param));
    }

    @PutMapping("/{paymentId}/status")
    @RequirePermission(menu = "PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<Void> updateStatus(@PathVariable Long paymentId, @Valid @RequestBody PaymentStatusUpdateReq req) {
        paymentService.updateStatus(paymentId, req.getStatus(), req.getFailReason());
        return ApiResponse.ok();
    }

    @Operation(summary = "이체파일 생성", description = "정산월+은행코드 기준 PENDING 지급건을 이체파일로 생성하고 SENT 상태로 갱신")
    @PostMapping("/transfer-file")
    @RequirePermission(menu = "PAYMENT", perm = "EXPORT")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<TransferFileResp> generateTransferFile(
            @Pattern(regexp = "\\d{6}", message = "settleMonth는 YYYYMM 형식이어야 합니다") @RequestParam String settleMonth,
            @RequestParam String bankCode) {
        return ApiResponse.ok(paymentService.generateTransferFile(settleMonth, bankCode));
    }

    @Operation(summary = "세금 단건 재계산")
    @PostMapping("/{paymentId}/calculate-tax")
    @RequirePermission(menu = "PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<Void> calculateTax(@PathVariable Long paymentId) {
        paymentService.calculateTaxes(paymentId);
        return ApiResponse.ok();
    }

    @Operation(summary = "세금 월별 일괄 재계산")
    @PostMapping("/calculate-taxes")
    @RequirePermission(menu = "PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<Void> calculateTaxesForMonth(
            @Pattern(regexp = "\\d{6}", message = "settleMonth는 YYYYMM 형식이어야 합니다") @RequestParam String settleMonth) {
        paymentService.calculateTaxesForMonth(settleMonth);
        return ApiResponse.ok();
    }

    @Operation(summary = "차감 단건 적용")
    @PostMapping("/{paymentId}/apply-deductions")
    @RequirePermission(menu = "PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<Void> applyDeductions(
            @PathVariable Long paymentId,
            @Pattern(regexp = "\\d{6}", message = "settleMonth는 YYYYMM 형식이어야 합니다") @RequestParam String settleMonth) {
        paymentService.applyDeductions(paymentId, settleMonth);
        return ApiResponse.ok();
    }

    @Operation(summary = "차감 월별 일괄 적용")
    @PostMapping("/apply-deductions")
    @RequirePermission(menu = "PAYMENT", perm = "UPDATE")
    @DataChangeLog(menu = "PAYMENT", table = "payment")
    public ApiResponse<Void> applyDeductionsForMonth(
            @Pattern(regexp = "\\d{6}", message = "settleMonth는 YYYYMM 형식이어야 합니다") @RequestParam String settleMonth) {
        paymentService.applyDeductionsForMonth(settleMonth);
        return ApiResponse.ok();
    }
}
