package com.example.demo;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.demo.department.model.DepartmentEntity;
import com.example.demo.directions.model.DirectionsEntity;
import com.example.demo.news.model.NewsEntity;
import com.example.demo.entrysData.model.EntrysDataEntity;
import com.example.demo.department.service.DepartmentService;
import com.example.demo.directions.service.DirectionsService;
import com.example.demo.entrysData.service.EntrysDataService;
import com.example.demo.news.service.NewsService;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
    private final Logger log = LoggerFactory.getLogger(DemoApplication.class);

    private final EntrysDataService entrysDataService;
    private final DepartmentService departmentService;
    private final NewsService newsService;
    private final DirectionsService directionsService;

    public DemoApplication(EntrysDataService entrysDataService, DepartmentService departmentService,
            NewsService newsService, DirectionsService directionsService) {
        this.entrysDataService = entrysDataService;
        this.departmentService = departmentService;
        this.directionsService = directionsService;
        this.newsService = newsService;
    }

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

    @Override
    public void run(String... args) throws Exception {
        if (args.length > 0 && Objects.equals("--populate", args[0])) {
            log.info("Create departments");
            final var dep1 = departmentService.create(new DepartmentEntity("ГУМ"));
            final var dep2 = departmentService.create(new DepartmentEntity("ИСТ"));
            final var dep3 = departmentService.create(new DepartmentEntity("ИЯ"));

            log.info("Create entrysData");
            entrysDataService.create(new EntrysDataEntity("login", "password", "user", dep2));
            entrysDataService.create(new EntrysDataEntity("login1", "password", "user", dep3));
            entrysDataService.create(new EntrysDataEntity("login2", "password", "user", dep1));

            log.info("Create directions");

            directionsService.create(new DirectionsEntity("09.02.23", "Программная инженерия", dep1, "Информатика"));
            directionsService.create(new DirectionsEntity("09.03.24", "ИВТ", dep2, "Информатика, математика"));
            directionsService.create(new DirectionsEntity("02.05.20", "ИСТ", dep3, "Информатика, иностранный языкы"));

            log.info("Create news");
            newsService.create(new NewsEntity("2024-04-15", "new1", "description1", dep2));
            newsService.create(new NewsEntity("2024-02-23", "new2", "description2", dep3));
            newsService.create(new NewsEntity("2024-03-17", "new3", "description3", dep1));
        }
    }
}
