package com.seer.teach.pay.api.wallet;

import com.seer.teach.common.exception.CommonException;
import com.seer.teach.pay.api.convert.AppPayAccountConvert;
import com.seer.teach.pay.api.wallet.dto.CoinConfigRespDTO;
import com.seer.teach.pay.api.wallet.dto.CoinToAmountRespDTO;
import com.seer.teach.pay.api.wallet.dto.UserCoinAccountBalanceReqDTO;
import com.seer.teach.pay.api.wallet.dto.UserCoinAccountRespDTO;
import com.seer.teach.pay.entity.CoinConfigEntity;
import com.seer.teach.pay.entity.PayCoinAccountEntity;
import com.seer.teach.pay.service.ICoinConfigService;
import com.seer.teach.pay.service.IPayCoinAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

import static com.seer.teach.common.enums.ResultCodeEnum.USER_COIN_AMOUNT_CANNOT_BE_NEGATIVE;

@RestController
@Validated
@Slf4j
@RequiredArgsConstructor
public class CoinAccountServiceApiImpl implements CoinAccountServiceApi {


    private final IPayCoinAccountService userCoinAccountService;

    private final ICoinConfigService coinConfigService;

    @Override
    public UserCoinAccountRespDTO getUserCoinAccount(Integer userId) {
        log.info("获取用户[{}]账户信息", userId);
        PayCoinAccountEntity payCoinAccountEntity = userCoinAccountService.getByUserId(userId);
        return AppPayAccountConvert.INSTANCE.convertOne(payCoinAccountEntity);
    }

    @Transactional(rollbackFor = Exception.class,  timeout = 60)
    @Override
    public boolean addBalance(UserCoinAccountBalanceReqDTO reqDTO) {
       Integer userId = reqDTO.getUserId();
       Integer accountId = reqDTO.getAccountId();
       BigDecimal amount = reqDTO.getAmount();
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            throw new CommonException(USER_COIN_AMOUNT_CANNOT_BE_NEGATIVE);
        }
        log.info("用户 {} 添加账户余额，金额: {} 分", userId, amount);
        return userCoinAccountService.addBalance(userId, accountId, amount);
    }

    @Transactional(rollbackFor = Exception.class, timeout = 120)
    @Override
    public boolean deductBalance(UserCoinAccountBalanceReqDTO reqDTO) {
        Integer userId = reqDTO.getUserId();
        Integer accountId = reqDTO.getAccountId();
        BigDecimal amount = reqDTO.getAmount();
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            throw new CommonException(USER_COIN_AMOUNT_CANNOT_BE_NEGATIVE);
        }
        log.info("用户 {} 扣除账户余额，金额: {} 分", userId, amount);
        return userCoinAccountService.deductBalance(userId, accountId, amount);
    }

    @Override
    public CoinToAmountRespDTO convertCoinToAmount(String coin) {
        BigDecimal coinBigDecimal = coinConfigService.convertCoinToAmount(new BigDecimal(coin));
        return new CoinToAmountRespDTO(coin, coinBigDecimal.toString());
    }

    @Override
    public CoinConfigRespDTO getActiveAndLatestCoinConfig() {
        CoinConfigEntity coinConfig = coinConfigService.getActiveAndLatestCoinConfig();
        return new CoinConfigRespDTO(coinConfig.getId(),coinConfig.getCoinName(), coinConfig.getExchangeRatio());
    }
}
