package com.crm.pay.service.impl;

import com.crm.pay.dao.repository.CustomInfoRepository;
import com.crm.pay.service.CustomFundHistoryService;
import com.crm.pay.service.CustomInfoService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.entity.custom.fund.history.CustomFundHistoryAddEntity;
import com.crm.rely.backend.model.pojo.table.CustomInfoTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class CustomInfoServiceImpl implements CustomInfoService {

    @Autowired
    private CustomInfoRepository customInfoRepository;

    @Autowired
    private CustomFundHistoryService customFundHistoryService;

    @Override
    public CustomInfoTable getById(Long customId) throws ServiceException {

        CustomInfoTable customInfoTable = customInfoRepository.getFirstById(customId);
        if (customInfoTable == null) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customInfoTable;

    }

    @Override
    public CustomInfoTable findFirstById(Long customId) throws ServiceException {

        CustomInfoTable customInfoTable = customInfoRepository.findFirstById(customId);
        if (customInfoTable == null) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customInfoTable;

    }

    @Override
    public CustomInfoTable getByCId(Long cId) throws ServiceException {
        CustomInfoTable customInfoTable = customInfoRepository.getFirstByCId(cId);
        if (customInfoTable == null) {
            throw new ServiceException(Constants.LOGIN_NOT_EXIST_ERROR);
        }
        return customInfoTable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWithdrawFeeReduction(Long cId, int number) throws ServiceException {
        CustomInfoTable table = customInfoRepository.findFirstByCId(cId);

// Initialize withdrawFeeReduction to 0 if it's null
        Integer withdrawFeeReduction = table.getWithdrawFeeReduction();
        if (withdrawFeeReduction == null) {
            withdrawFeeReduction = 0;
        }

        int reductionNumber = withdrawFeeReduction + number;

// Check if the new reduction number is valid for both positive and negative changes
        if (reductionNumber >= 0) {
            table.setWithdrawFeeReduction(reductionNumber);
            customInfoRepository.save(table);
            return 1;
        }

        return 0;
    }

    @Override
    public int getWithdrawFeeReduction(Long cId) throws ServiceException {

        CustomInfoTable table = customInfoRepository.getFirstByCId(cId);
        return table.getWithdrawFeeReduction() == null ? 0 : table.getWithdrawFeeReduction();
    }

    private void addHistory(CustomInfoTable customInfoTable, Integer sourceType, BigDecimal amount, String remarks) throws ServiceException {
        CustomFundHistoryAddEntity entity = new CustomFundHistoryAddEntity();
        entity.setCustomId(customInfoTable.getId());
        entity.setCId(customInfoTable.getCId());
        entity.setAgentId(customInfoTable.getAgentId());
        entity.setAmount(amount);
        entity.setRemarks(remarks);
        entity.setSourceType(sourceType);
        customFundHistoryService.add(entity);
    }
}
