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.TariffTimeTerm;
import ru.soune.nocopy.entity.tarif.TariffType;
import ru.soune.nocopy.repository.TariffRepository;

import java.util.Arrays;
import java.util.List;

@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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case DEMO:
                tariff.setName("demo");
                tariff.setPrice(0);
                tariff.setTokens(30);
                tariff.setMaxFilesCount(10);
                tariff.setDiskSize(1024L * 1024 * 100);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case START_YEAR:
                tariff.setName("start_year");
                tariff.setPrice(1100);
                tariff.setTokens(300);
                tariff.setMaxFilesCount(80);
                tariff.setDiskSize(1024L * 1024 * 500);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case PRO_YEAR:
                tariff.setName("pro_year");
                tariff.setPrice(4100);
                tariff.setTokens(900);
                tariff.setMaxFilesCount(300);
                tariff.setDiskSize(1024L * 1024 * 1024 * 10L);
                tariff.setMaxUsers(1L);
                tariff.setTariffAccountType("b2c");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case STUDIO_YEAR:
                tariff.setName("studio_year");
                tariff.setPrice(9500);
                tariff.setTokens(3000);
                tariff.setMaxFilesCount(1000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(2L);
                tariff.setTariffAccountType("b2c");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case BASIC_YEAR:
                tariff.setName("basic_year");
                tariff.setPrice(basicPrice);
                tariff.setTokens(basicTokens);
                tariff.setMaxFilesCount(enterpriceMaxFileCount);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(basicUserCount);
                tariff.setTariffAccountType("b2c");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case ENTERPRISE_YEAR:
                tariff.setName("enterprise_year");
                tariff.setPrice(200000);
                tariff.setTokens(30000);
                tariff.setMaxFilesCount(100000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 10L);
                tariff.setMaxUsers(10L);
                tariff.setTariffAccountType("b2b");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case BUISNES_YEAR:
                tariff.setName("buisnes_year");
                tariff.setPrice(29000);
                tariff.setTokens(5000);
                tariff.setMaxFilesCount(5000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(5L);
                tariff.setTariffAccountType("b2b");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                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");
                tariff.setTariffTerm(TariffTimeTerm.MONTHLY);
                break;

            case CORPORAT_YEAR:
                tariff.setName("corporat_year");
                tariff.setPrice(99000);
                tariff.setTokens(20000);
                tariff.setMaxFilesCount(50000);
                tariff.setDiskSize(1024L * 1024 * 1024 * 50L);
                tariff.setMaxUsers(10L);
                tariff.setTariffAccountType("b2b");
                tariff.setTariffTerm(TariffTimeTerm.YEAR);
                break;


            case TOKEN_100:
                tariff.setName("token_100");
                tariff.setPrice(299);
                tariff.setTokens(100);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("token");
                break;

            case TOKEN_500:
                tariff.setName("token_500");
                tariff.setPrice(1299);
                tariff.setTokens(500);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("token");
                break;

            case TOKEN_1000:
                tariff.setName("token_1000");
                tariff.setPrice(2399);
                tariff.setTokens(1000);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("token");
                break;

            case TOKEN_5000:
                tariff.setName("token_5000");
                tariff.setPrice(9999);
                tariff.setTokens(5000);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("token");
                break;


            case MONITORING_DAILY:
                tariff.setName("monitoring_daily");
                tariff.setPrice(0);
                tariff.setTokens(10);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("monitoring");
                break;

            case MONITORING_WEEKLY:
                tariff.setName("monitoring_weekly");
                tariff.setPrice(0);
                tariff.setTokens(25);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("monitoring");
                break;

            case MONITORING_MONTHLY:
                tariff.setName("monitoring_monthly");
                tariff.setPrice(0);
                tariff.setTokens(50);
                tariff.setMaxFilesCount(0);
                tariff.setDiskSize(0L);
                tariff.setMaxUsers(0L);
                tariff.setTariffAccountType("monitoring");
                break;
        }

        tariffRepository.save(tariff);
    }
}
