package com.rit.ems.config;

import com.rit.ems.model.*;
import com.rit.ems.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Component
public class DataSeeder implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ClassMappingRepository classMappingRepository;

    @Autowired
    private VenueRepository venueRepository;

    @Autowired
    private InstitutionalEventRepository institutionalEventRepository;

    @Override
    public void run(String... args) throws Exception {
        seedUsers();
        seedClasses();
        seedVenues();
        seedInstitutionalEvents();
    }

    private void seedUsers() {
        if (userRepository.count() == 0) {
            System.out.println("[DataSeeder] Seeding default users...");
            List<User> defaultUsers = Arrays.asList(
                new User(1L, "admin@rit.edu", "admin123", "System Administrator", "ADMIN", "ADMIN", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(2L, "faculty@rit.edu", "faculty123", "Dr. Faculty Member", "FACULTY", "CSE", false, false, true, 0, "CSE", "3rd Year", "A", "3rd Year", "A", null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(3L, "hod@rit.edu", "hod123", "Prof. Head of Dept", "HOD", "AI&ML", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(4L, "principal@rit.edu", "principal123", "Dr. College Principal", "PRINCIPAL", "ADMIN", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(5L, "placement@rit.edu", "placement123", "Placement Coordinator", "PLACEMENT", "Placement Department", false, true, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(6L, "admin2@rit.edu", "admin123", "Secondary Admin", "ADMIN", "ADMIN", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(7L, "principal2@rit.edu", "principal123", "Vice Principal", "PRINCIPAL", "ADMIN", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(8L, "hod_cse@rit.edu", "hod123", "CSE HOD", "HOD", "CSE", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>()),
                new User(9L, "faculty2@rit.edu", "faculty123", "Assistant Professor CSE", "FACULTY", "CSE", false, false, false, 0, null, null, null, null, null, null, "", "Male", "Rajalakshmi Institute of Technology", new ArrayList<>())
            );
            userRepository.saveAll(defaultUsers);
        }
    }

    private void seedClasses() {
        if (classMappingRepository.count() == 0) {
            System.out.println("[DataSeeder] Seeding default classes...");
            List<ClassMapping> defaultClasses = Arrays.asList(
                new ClassMapping(1L, "RIT", "CSE", "3rd Year", Arrays.asList("A", "B"), "Ready"),
                new ClassMapping(2L, "RIT", "AI&ML", "3rd Year", Collections.singletonList("A"), "Ready"),
                new ClassMapping(3L, "RIT", "ECE", "2nd Year", Arrays.asList("A", "B", "C"), "Ready")
            );
            classMappingRepository.saveAll(defaultClasses);
        }
    }

    private void seedVenues() {
        if (venueRepository.count() == 0) {
            System.out.println("[DataSeeder] Seeding default venues...");
            List<Venue> defaultVenues = Arrays.asList(
                new Venue("v-1", "GB 4th floor auditorium", 500),
                new Venue("v-2", "Wozniak Auditorium", 180),
                new Venue("v-3", "C6-02 Indoor Theatre", 180),
                new Venue("v-4", "H Block Guest Lecture Theatre", 60),
                new Venue("v-5", "Steve Jobs Computer Centre 1", 120),
                new Venue("v-6", "Steve Jobs Computer Centre 2", 120)
            );
            venueRepository.saveAll(defaultVenues);
        }
    }

    private void seedInstitutionalEvents() {
        if (institutionalEventRepository.count() == 0) {
            System.out.println("[DataSeeder] Seeding institutional events...");
            List<InstitutionalEvent> defaultEvents = Arrays.asList(
                new InstitutionalEvent("ie-1", "New Year & Christmas Celebration", "January", "EVEN"),
                new InstitutionalEvent("ie-2", "Republic Day Celebration", "January", "EVEN"),
                new InstitutionalEvent("ie-3", "Pongal Celebration", "January", "EVEN"),
                new InstitutionalEvent("ie-4", "Yatra Banner Release", "January", "EVEN"),
                new InstitutionalEvent("ie-5", "Yatra Ethnic Day", "February", "EVEN"),
                new InstitutionalEvent("ie-6", "Yatra", "February", "EVEN"),
                new InstitutionalEvent("ie-7", "International Womens Day", "March", "EVEN"),
                new InstitutionalEvent("ie-8", "Ugathi", "March", "EVEN"),
                new InstitutionalEvent("ie-9", "Tamil New Year", "April", "EVEN"),
                new InstitutionalEvent("ie-10", "Techritz", "July", "ODD"),
                new InstitutionalEvent("ie-11", "Independence Day", "August", "ODD"),
                new InstitutionalEvent("ie-12", "AI Horizon Week", "August", "ODD"),
                new InstitutionalEvent("ie-13", "Initiation Day", "September", "ODD"),
                new InstitutionalEvent("ie-14", "Onam Celebration", "September", "ODD"),
                new InstitutionalEvent("ie-15", "Teachers Day", "September", "ODD"),
                new InstitutionalEvent("ie-16", "Engineers Day", "September", "ODD"),
                new InstitutionalEvent("ie-17", "SDG Golu", "September", "ODD"),
                new InstitutionalEvent("ie-18", "Ayutha Puja", "October", "ODD"),
                new InstitutionalEvent("ie-19", "Tech Fest IIT Bombay", "October", "ODD"),
                new InstitutionalEvent("ie-20", "EDI Conclave - FICCI Flow", "November", "ODD")
            );
            institutionalEventRepository.saveAll(defaultEvents);
        }
    }
}
