package com.platform2easy.genesis.domain.mock;

import com.platform2easy.genesis.domain.enums.UserRole;
import com.platform2easy.genesis.domain.model.User2Easy;
import com.platform2easy.genesis.domain.model.cerc.ContratoValorCpr;
import com.platform2easy.genesis.domain.model.cerc.EmissaoCpr;
import com.platform2easy.genesis.domain.model.cerc.SolicitacaoRegistroCpr;
import com.platform2easy.genesis.domain.model.cerc.StatusSolicitacaoRegistroCpr;
import com.platform2easy.genesis.domain.model.cerc.TipoCpr;
import com.platform2easy.genesis.domain.repository.UserRepository;
import com.platform2easy.genesis.domain.repository.cerc.ContratoValorCprRepository;
import com.platform2easy.genesis.domain.repository.cerc.EmissaoCprRepository;
import com.platform2easy.genesis.domain.repository.cerc.SolicitacaoRegistroCprRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;

@Component
@Order(1)
public class DatabaseFake implements CommandLineRunner {

    private final UserRepository userRepository;
    private final EmissaoCprRepository emissaoCprRepository;
    private final ContratoValorCprRepository contratoValorCprRepository;
    private final SolicitacaoRegistroCprRepository solicitacaoRegistroCprRepository;

    public DatabaseFake(UserRepository userRepository, EmissaoCprRepository emissaoCprRepository, ContratoValorCprRepository contratoValorCprRepository, SolicitacaoRegistroCprRepository solicitacaoRegistroCprRepository) {
        this.userRepository = userRepository;
        this.emissaoCprRepository = emissaoCprRepository;
        this.contratoValorCprRepository = contratoValorCprRepository;
        this.solicitacaoRegistroCprRepository = solicitacaoRegistroCprRepository;
    }


    @Override
    public void run(String... args) {
        var contrato = new ContratoValorCpr();
        contrato.setCodigoContratoOperacaoCredito("TF123");
        contrato.setValorTotalCredito(new BigDecimal(10000));
        contrato.setValorLiquidoCredito(new BigDecimal(10000));
        contrato.setNaturezaOperacao("YU899");
        contrato.setDataTransacao(LocalDate.now());
        contratoValorCprRepository.save(contrato);

        var emissao = new EmissaoCpr();
        emissao.setLocalEmissao("Paraná");
        emissao.setDataEntrega(LocalDate.now());
        emissao.setRazaoSocialEmissor("TOO EASY TRADING COMMODITIES LTDA");
        emissao.setTipoCpr(TipoCpr.FINANCEIRO);
        emissaoCprRepository.save(emissao);

        var registro = new SolicitacaoRegistroCpr();
        registro.setStatus(StatusSolicitacaoRegistroCpr.NOVO);
        registro.setAditivo("Milho seco em grãos, padrão comercial tipo exportação");
        registro.setContratoValorCpr(contrato);
        registro.setEmissaoCpr(emissao);
        solicitacaoRegistroCprRepository.save(registro);

        userRepository.save(new User2Easy(null, "admin", new BCryptPasswordEncoder().encode("admin"), "Administrador", UserRole.ADMIN));
        userRepository.save(new User2Easy(null, "operador", new BCryptPasswordEncoder().encode("1234"), "Operador Escrow", UserRole.ESCROW_USER));
        userRepository.save(new User2Easy(null, "produtor", new BCryptPasswordEncoder().encode("1234"), "Produtor", UserRole.TRADER_USER));
        userRepository.save(new User2Easy(null, "corretor", new BCryptPasswordEncoder().encode("1234"), "Corretor", UserRole.BROKER_USER));
    }
}
