package com.writeoff.module.finance.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.finance.dto.ConfirmPaymentRequest;
import com.writeoff.module.finance.dto.FinanceLockRequest;
import com.writeoff.module.finance.dto.FinanceReconciliationRequest;
import com.writeoff.module.finance.dto.UpsertFinanceMeetingBillRequest;
import com.writeoff.module.finance.model.FinanceMeetingBillInfo;
import com.writeoff.module.finance.model.Payment;
import com.writeoff.module.finance.service.FinanceService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/finance")
public class FinanceController {
    private final FinanceService financeService;

    public FinanceController(FinanceService financeService) {
        this.financeService = financeService;
    }

    @GetMapping("/projects")
    public ApiResponse<PageResult<Payment>> projects() {
        return ApiResponse.success(financeService.listProjects());
    }

    @PostMapping("/payments")
    @RequirePermission(value = "finance.payment.confirm", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_PAYMENT_CONFIRM")
    public ApiResponse<Map<String, Object>> confirmPayment(@RequestBody @Valid ConfirmPaymentRequest request) {
        return ApiResponse.success(financeService.confirmPayment(request));
    }

    @GetMapping("/ledger/export")
    @RequirePermission(value = "finance.ledger.export", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_LEDGER_EXPORT")
    public ApiResponse<Map<String, Object>> exportLedger() {
        return ApiResponse.success(financeService.exportLedger());
    }

    @PostMapping("/reconciliation")
    @RequirePermission(value = "finance.reconciliation", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_RECONCILIATION")
    public ApiResponse<Map<String, Object>> reconciliation(@RequestBody @Valid FinanceReconciliationRequest request) {
        return ApiResponse.success(financeService.reconciliation(request));
    }

    @PostMapping("/lock")
    @RequirePermission(value = "finance.lock", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_LOCK")
    public ApiResponse<Map<String, Object>> lock(@RequestBody @Valid FinanceLockRequest request) {
        return ApiResponse.success(financeService.lock(request));
    }

    @PostMapping("/unlock")
    @RequirePermission(value = "finance.unlock", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_UNLOCK")
    public ApiResponse<Map<String, Object>> unlock(@RequestBody @Valid FinanceLockRequest request) {
        return ApiResponse.success(financeService.unlock(request));
    }

    @GetMapping("/reconciliation/list")
    @RequirePermission(value = "finance.reconciliation", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_RECONCILIATION_LIST")
    public ApiResponse<Map<String, Object>> reconciliationList(@RequestParam(value = "projectId", required = false) Long projectId) {
        return ApiResponse.success(financeService.reconciliationList(projectId));
    }

    @GetMapping("/meeting-bills")
    @RequirePermission(value = "finance.reconciliation", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_MEETING_BILL_LIST")
    public ApiResponse<PageResult<FinanceMeetingBillInfo>> meetingBills(@RequestParam(value = "projectId", required = false) Long projectId) {
        return ApiResponse.success(financeService.listMeetingBills(projectId));
    }

    @PostMapping("/meeting-bills")
    @RequirePermission(value = "finance.reconciliation", dataScope = DataScopeType.PROJECT, auditAction = "FINANCE_MEETING_BILL_UPSERT")
    public ApiResponse<FinanceMeetingBillInfo> upsertMeetingBill(@RequestBody @Valid UpsertFinanceMeetingBillRequest request) {
        return ApiResponse.success(financeService.upsertMeetingBill(request));
    }
}
