package de.szut.casino;

import de.szut.casino.lootboxes.LootBoxEntity;
import de.szut.casino.lootboxes.LootBoxRepository;
import de.szut.casino.lootboxes.RewardEntity;
import de.szut.casino.lootboxes.RewardRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

@SpringBootApplication
public class CasinoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CasinoApplication.class, args);
    }

    @Bean
    public static RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public static JavaMailSenderImpl javaMailSenderImpl() {
        return new JavaMailSenderImpl();
    }

    @Bean
    public CommandLineRunner initData(LootBoxRepository lootBoxRepository, RewardRepository rewardRepository) {
        return _ -> {
            if (lootBoxRepository.count() == 0) {
                LootBoxEntity basicLootBox = new LootBoxEntity();
                basicLootBox.setName("Basic LootBox");
                basicLootBox.setPrice(new BigDecimal("2"));
                basicLootBox.setRewards(new ArrayList<>()); // Initialize the list

                LootBoxEntity premiumLootBox = new LootBoxEntity();
                premiumLootBox.setName("Premium LootBox");
                premiumLootBox.setPrice(new BigDecimal("5"));
                premiumLootBox.setRewards(new ArrayList<>()); // Initialize the list

                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));

                RewardEntity commonReward = new RewardEntity();
                commonReward.setValue(new BigDecimal("0.50"));
                commonReward.setProbability(new BigDecimal("0.7"));

                RewardEntity rareReward = new RewardEntity();
                rareReward.setValue(new BigDecimal("2.00"));
                rareReward.setProbability(new BigDecimal("0.25"));

                RewardEntity epicReward = new RewardEntity();
                epicReward.setValue(new BigDecimal("5.00"));
                epicReward.setProbability(new BigDecimal("0.5"));

                RewardEntity premiumCommon = new RewardEntity();
                premiumCommon.setValue(new BigDecimal("2.00"));
                premiumCommon.setProbability(new BigDecimal("0.6"));

                RewardEntity premiumRare = new RewardEntity();
                premiumRare.setValue(new BigDecimal("5.00"));
                premiumRare.setProbability(new BigDecimal("0.3"));

                RewardEntity legendaryReward = new RewardEntity();
                legendaryReward.setValue(new BigDecimal("15.00"));
                legendaryReward.setProbability(new BigDecimal("0.10"));

                rewardRepository.saveAll(Arrays.asList(
                        commonReward, rareReward, epicReward,
                        premiumCommon, premiumRare, legendaryReward
                ));

                basicLootBox.getRewards().add(commonReward);
                basicLootBox.getRewards().add(premiumRare);

                premiumLootBox.getRewards().add(premiumCommon);
                premiumLootBox.getRewards().add(premiumRare);
                premiumLootBox.getRewards().add(legendaryReward);

                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));

                System.out.println("Initial LootBoxes and rewards created successfully");
            } else {
                System.out.println("LootBoxes already exist, skipping initialization");
            }
        };
    }
}
