package de.nilzbu.mytimetracker.config;

import de.nilzbu.mytimetracker.model.DayStatus;
import de.nilzbu.mytimetracker.model.TimeEntry;
import de.nilzbu.mytimetracker.model.User;
import de.nilzbu.mytimetracker.repository.TimeEntryRepository;
import de.nilzbu.mytimetracker.repository.UserRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Component;

import java.time.*;
import java.util.*;

@Component
public class SeedDataGenerator {

    private final TimeEntryRepository timeEntryRepository;
    private final UserRepository userRepository;

    public SeedDataGenerator(TimeEntryRepository timeEntryRepository, UserRepository userRepository) {
        this.timeEntryRepository = timeEntryRepository;
        this.userRepository = userRepository;
    }

    @PostConstruct
    public void generate() {
        Optional<User> userOpt = userRepository.findByUsername("admin"); // passe ggf. an
        if (userOpt.isEmpty()) return;

        User user = userOpt.get();
        Random random = new Random();

        for (int year = 2020; year <= 2025; year++) {
            Set<LocalDate> allWorkDays = new HashSet<>();
            LocalDate start = LocalDate.of(year, 1, 1);
            LocalDate end = year == LocalDate.now().getYear() ? LocalDate.now() : LocalDate.of(year, 12, 31);

            for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
                DayOfWeek dow = date.getDayOfWeek();
                if (dow != DayOfWeek.SATURDAY && dow != DayOfWeek.SUNDAY) {
                    allWorkDays.add(date);
                }
            }

            List<LocalDate> workDays = new ArrayList<>(allWorkDays);
            Collections.shuffle(workDays);

            List<LocalDate> vacationDays = workDays.subList(0, Math.min(30, workDays.size()));
            List<LocalDate> sickDays = workDays.subList(30, Math.min(45, workDays.size()));

            for (LocalDate date : allWorkDays) {
                if (vacationDays.contains(date)) {
                    saveEntry(user, date, null, null, 0, 0, DayStatus.VACATION, "Urlaub");
                } else if (sickDays.contains(date)) {
                    saveEntry(user, date, null, null, 0, 0, DayStatus.SICK, "Krank");
                } else {
                    LocalTime startTime = LocalTime.of(8, 0).plusMinutes(random.nextInt(90)); // 08:00–09:30
                    int workMinutes = 450 + random.nextInt(61); // 450–510
                    int pause = 30 + random.nextInt(31); // 30–60 min Pause
                    LocalTime endTime = startTime.plusMinutes(workMinutes + pause);
                    int target = 480;
                    DayStatus status = random.nextDouble() < 0.03 ? DayStatus.OFFICE : DayStatus.REMOTE;

                    saveEntry(user, date, startTime, endTime, pause, target, status, null);
                }
            }
        }
    }

    private void saveEntry(User user, LocalDate date, LocalTime start, LocalTime end, int pause, int target, DayStatus status, String comment) {
        if (timeEntryRepository.findByUserAndDate(user, date).isPresent()) return;

        TimeEntry entry = TimeEntry.builder()
                .user(user)
                .date(date)
                .startTime(start)
                .endTime(end)
                .pauseMinutes(pause)
                .targetMinutes(target)
                .status(status)
                .comment(comment)
                .build();
        timeEntryRepository.save(entry);
        System.out.println("Inserted entry for: " + date + " [" + status + "]");
    }
}