package com.erp.tenant.service.finance.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.tenant.dto.finance.FinancePageResponse;
import com.erp.tenant.entity.finance.FinanceRecord;
import com.erp.tenant.entity.finance.Reconciliation;
import com.erp.tenant.repository.finance.FinanceRecordRepository;
import com.erp.tenant.repository.finance.ReconciliationRepository;
import com.erp.tenant.service.finance.FinanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import com.erp.common.core.model.PageResponse;

/**
 * 租户端财务服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FinanceServiceImpl implements FinanceService {

    private final FinanceRecordRepository financeRecordRepository;
    private final ReconciliationRepository reconciliationRepository;

    @Override
    public IPage<FinanceRecord> getFinancePage(Long tenantId, String type, Integer status,
                                                String startDate, String endDate,
                                                int page, int pageSize) {
        return financeRecordRepository.findPage(
                new Page<>(page, pageSize),
                tenantId, type, status, startDate, endDate
        );
    }

    @Override
    public FinancePageResponse.FinanceSummary getFinanceSummary(Long tenantId, String startDate, String endDate) {
        BigDecimal totalIncome = financeRecordRepository.sumAmountByType(tenantId, FinanceRecord.TYPE_INCOME, startDate, endDate);
        BigDecimal totalExpense = financeRecordRepository.sumAmountByType(tenantId, FinanceRecord.TYPE_EXPENSE, startDate, endDate);

        return FinancePageResponse.FinanceSummary.builder()
                .totalIncome(totalIncome != null ? totalIncome : BigDecimal.ZERO)
                .totalExpense(totalExpense != null ? totalExpense : BigDecimal.ZERO)
                .netProfit((totalIncome != null ? totalIncome : BigDecimal.ZERO)
                        .subtract(totalExpense != null ? totalExpense : BigDecimal.ZERO))
                .build();
    }

    @Override
    public FinanceRecord getFinanceDetail(Long tenantId, Long recordId) {
        return financeRecordRepository.findById(recordId, tenantId);
    }

    @Override
    @Transactional
    public boolean confirmRecord(Long tenantId, Long recordId, Long operatorId, String remark) {
        int rows = financeRecordRepository.updateStatus(recordId, tenantId,
                FinanceRecord.STATUS_CONFIRMED, operatorId, remark);
        return rows > 0;
    }

    @Override
    @Transactional
    public boolean cancelRecord(Long tenantId, Long recordId, Long operatorId, String remark) {
        int rows = financeRecordRepository.updateStatus(recordId, tenantId,
                FinanceRecord.STATUS_CANCELLED, operatorId, remark);
        return rows > 0;
    }

    @Override
    public IPage<Reconciliation> getReconciliationPage(Long tenantId, Integer status, String keyword,
                                                       int page, int pageSize) {
        return reconciliationRepository.findPage(
                new Page<>(page, pageSize),
                tenantId, status, keyword
        );
    }

    @Override
    public Reconciliation getReconciliationDetail(Long tenantId, Long reconciliationId) {
        return reconciliationRepository.findById(reconciliationId, tenantId);
    }

    @Override
    @Transactional
    public boolean completeReconciliation(Long tenantId, Long reconciliationId, Long operatorId, String remark) {
        // TODO: update reconciliation status
        return true;
    }
}
