package com.ga.api.controller.installment;

import com.ga.api.service.installment.InstallmentService;
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.installment.InstallmentPlanResp;
import com.ga.core.vo.installment.InstallmentPlanSearchParam;
import com.ga.core.vo.installment.InstallmentPlanVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "분급 계획 관리")
@RestController
@RequestMapping("/api/installment-plans")
@RequiredArgsConstructor
public class InstallmentController {

    private final InstallmentService installmentService;

    @GetMapping
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "READ")
    public ApiResponse<PageResponse<InstallmentPlanResp>> list(InstallmentPlanSearchParam param) {
        return ApiResponse.ok(installmentService.list(param));
    }

    @GetMapping("/{planId}")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "READ")
    public ApiResponse<InstallmentPlanResp> detail(@PathVariable Long planId) {
        return ApiResponse.ok(installmentService.detail(planId));
    }

    @PostMapping("/generate")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "CREATE")
    @DataChangeLog(menu = "INSTALLMENT_PLAN", table = "installment_plan")
    public ApiResponse<Void> generate(@Valid @RequestBody GeneratePlanReq req) {
        installmentService.createPlan(
                req.getContractId(),
                req.getTotalCommission(),
                req.getContractDate(),
                req.getProductCategory()
        );
        return ApiResponse.ok();
    }

    @PutMapping("/{planId}/pay")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "UPDATE")
    @DataChangeLog(menu = "INSTALLMENT_PLAN", table = "installment_plan")
    public ApiResponse<Void> pay(@PathVariable Long planId,
                                 @Valid @RequestBody PayPlanReq req) {
        installmentService.payPlan(planId, req.getPaymentId(), req.getPaidAmount());
        return ApiResponse.ok();
    }

    @PutMapping("/{planId}/defer")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "UPDATE")
    @DataChangeLog(menu = "INSTALLMENT_PLAN", table = "installment_plan")
    public ApiResponse<Void> defer(@PathVariable Long planId) {
        installmentService.deferPlan(planId);
        return ApiResponse.ok();
    }

    @PutMapping("/{planId}/cancel")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "UPDATE")
    @DataChangeLog(menu = "INSTALLMENT_PLAN", table = "installment_plan")
    public ApiResponse<Void> cancel(@PathVariable Long planId) {
        installmentService.cancelPlan(planId);
        return ApiResponse.ok();
    }

    @GetMapping("/scheduled")
    @RequirePermission(menu = "INSTALLMENT_PLAN", perm = "READ")
    public ApiResponse<List<InstallmentPlanVO>> scheduled(@RequestParam String settleMonth) {
        return ApiResponse.ok(installmentService.selectScheduledForMonth(settleMonth));
    }

}
