package com.petshop.backend.config;

import com.petshop.backend.repository.PetRepository;
import com.petshop.backend.repository.ProductRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
@Profile("local")
public class LocalCatalogSeedInitializer implements CommandLineRunner {

    private final DataSource dataSource;
    private final PetRepository petRepository;
    private final ProductRepository productRepository;

    public LocalCatalogSeedInitializer(DataSource dataSource, PetRepository petRepository, ProductRepository productRepository) {
        this.dataSource = dataSource;
        this.petRepository = petRepository;
        this.productRepository = productRepository;
    }

    @Override
    public void run(String... args) {
        if (petRepository.count() > 6 || productRepository.count() > 6) {
            return;
        }

        ResourceDatabasePopulator populator = new ResourceDatabasePopulator(false, false, "UTF-8",
                new ClassPathResource("dev/expand_pet_product_seed.sql"));
        populator.execute(dataSource);
    }
}
