package dev.fyloz.trial.colorrecipesexplorer.config;

import dev.fyloz.trial.colorrecipesexplorer.config.properties.MaterialTypeProperties;
import dev.fyloz.trial.colorrecipesexplorer.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.service.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.service.MaterialTypeService;
import dev.fyloz.trial.colorrecipesexplorer.service.model.MaterialJavaService;
import dev.fyloz.trial.colorrecipesexplorer.service.model.MaterialTypeJavaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class InitialDataLoader implements ApplicationListener<ApplicationReadyEvent> {
    private final MaterialTypeService materialTypeService;
    private final MaterialTypeProperties materialTypeProperties;

    @Autowired
    public InitialDataLoader(MaterialTypeService materialTypeService, MaterialTypeProperties materialTypeProperties) {
        this.materialTypeService = materialTypeService;
        this.materialTypeProperties = materialTypeProperties;
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        materialTypeService.saveSystemTypes(materialTypeProperties.getSystemTypes());
    }
}
