package ru.soune.nocopy.service.tariff;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.soune.nocopy.entity.tarif.Tariff;
import ru.soune.nocopy.entity.tarif.TariffType;
import ru.soune.nocopy.repository.TariffRepository;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TariffInitializationService {

    @Value("${tarif.demo-price}")
    private long demoPrice;

    @Value("${tarif.demo-tokens}")
    private int demoTokens;

    @Value("${tarif.demo-user-count}")
    private long demoUserCount;

    @Value("${tarif.demo-max-files-count}")
    private int demoMaxFileCount;

    @Value("${tarif.start-price}")
    private long startPrice;

    @Value("${tarif.start-tokens}")
    private int startTokens;

    @Value("${tarif.start-max-files-count}")
    private int startMaxFileCount;

    @Value("${tarif.start-user-count}")
    private long startUserCount;

    @Value("${tarif.basic-price}")
    private long basicPrice;

    @Value("${tarif.basic-tokens}")
    private int basicTokens;

    @Value("${tarif.basic-max-files-count}")
    private int basicMaxFileCount;

    @Value("${tarif.basic-user-count}")
    private long basicUserCount;

    @Value("${tarif.pro-price}")
    private long proPrice;

    @Value("${tarif.pro-tokens}")
    private int proTokens;

    @Value("${tarif.pro-max-files-count}")
    private int proMaxFileCount;

    @Value("${tarif.pro-user-count}")
    private long proUserCount;

    @Value("${tarif.enterprice-price}")
    private long enterpricePrice;

    @Value("${tarif.enterprice-tokens}")
    private int enterpriceTokens;

    @Value("${tarif.enterprice-max-files-count}")
    private int enterpriceMaxFileCount;

    @Value("${tarif.enterprice-user-count}")
    private long enterpriceUserCount;

    private final TariffRepository tariffRepository;

    @EventListener(ApplicationReadyEvent.class)
    @Transactional
    public void initializeDefaultTariffs() {
        List<Tariff> existingTariffs = tariffRepository.findAll();

        if (existingTariffs.isEmpty()) {
            createDefaultTariffs();
        }
    }

    private void createDefaultTariffs() {
        Arrays.stream(TariffType.values())
                .forEach(this::createDefaultTariff);
    }

    private void createDefaultTariff(TariffType type) {
        Tariff tariff = new Tariff();
        tariff.setType(type.name());

        switch (type) {
            case FREE:
                tariff.setName("free");
                tariff.setPrice(0);
                tariff.setTokens(30);
                tariff.setMaxFilesCount(10);
                tariff.setDiskSize(1024L * 1024 * 100);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                break;

            case START:
                tariff.setName("start");
                tariff.setPrice(1200);
                tariff.setTokens(300);
                tariff.setMaxFilesCount(80);
                tariff.setDiskSize(1024L * 1024 * 500);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                break;

            case PRO:
                tariff.setName("pro");
                tariff.setPrice(4250);
                tariff.setTokens(900);
                tariff.setMaxFilesCount(300);
                tariff.setDiskSize(1024L * 1024 * 1024 * 10L);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                break;

            case STUDIO:
                tariff.setName("studio");
                tariff.setPrice(9750);
                tariff.setTokens(3000);
                tariff.setMaxFilesCount(1000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(2L);
                tariff.setTariffAccountType("b2c");
                break;

            case BASIC:
                tariff.setName("basic");
                tariff.setPrice(basicPrice);
                tariff.setTokens(basicTokens);
                tariff.setMaxFilesCount(enterpriceMaxFileCount);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(basicUserCount);
                tariff.setTariffAccountType("b2c");
                break;

            case ENTERPRISE:
                tariff.setName("enterprise");
                tariff.setPrice(200000);
                tariff.setTokens(30000);
                tariff.setMaxFilesCount(100000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 10L);
                tariff.setMaxUsers(10L);
                tariff.setTariffAccountType("b2b");
                break;

            case BUISNES:
                tariff.setName("buisnes");
                tariff.setPrice(29000);
                tariff.setTokens(5000);
                tariff.setMaxFilesCount(5000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(5L);
                tariff.setTariffAccountType("b2b");
                break;

            case CORPORAT:
                tariff.setName("corporat");
                tariff.setPrice(99000);
                tariff.setTokens(20000);
                tariff.setMaxFilesCount(50000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(10L);
                tariff.setTariffAccountType("b2b");
                break;
        }

        tariffRepository.save(tariff);
    }
}
