package ru.shine.promo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Component
public class AppProperties {

    private static final long LAMPORTS_PER_SOL = 1_000_000_000L;

    @Value("${solana.rpc.url}")
    private String solanaRpcUrl;

    @Value("${solana.sender.keypair-file}")
    private String solanaSenderKeypairFile;

    @Value("${promo.transfer.amount-sol}")
    private BigDecimal promoTransferAmountSol;

    @Value("${promo.codes.file}")
    private String promoCodesFile;

    @Value("${promo.used.file}")
    private String promoUsedFile;

    @Value("${promo.explorer.tx-url-template}")
    private String promoExplorerTxUrlTemplate;

    @Value("${promo.eternal-code.enabled:false}")
    private boolean promoEternalCodeEnabled;

    @Value("${promo.eternal-code.value:0000}")
    private String promoEternalCodeValue;

    public String getSolanaRpcUrl() {
        return solanaRpcUrl;
    }

    public String getSolanaSenderKeypairFile() {
        return solanaSenderKeypairFile;
    }

    public BigDecimal getPromoTransferAmountSol() {
        return promoTransferAmountSol;
    }

    public String getPromoCodesFile() {
        return promoCodesFile;
    }

    public String getPromoUsedFile() {
        return promoUsedFile;
    }

    public String getPromoExplorerTxUrlTemplate() {
        return promoExplorerTxUrlTemplate;
    }

    public boolean isPromoEternalCodeEnabled() {
        return promoEternalCodeEnabled;
    }

    public String getPromoEternalCodeValue() {
        if (promoEternalCodeValue == null) {
            return "";
        }
        return promoEternalCodeValue.trim().toLowerCase();
    }

    public long getPromoTransferAmountLamports() {
        BigDecimal lamports = promoTransferAmountSol.multiply(BigDecimal.valueOf(LAMPORTS_PER_SOL));
        return lamports.setScale(0, RoundingMode.UNNECESSARY).longValueExact();
    }
}
