package ical

import (
	"htwkalender/ical/model"
	"htwkalender/ical/service/functions"
	clock "htwkalender/ical/service/functions/time"
	"math/rand"
	"time"
)

// generateTestEvents generates test events from the last 7 days to the next 7 days for the given modules
func generateTestEvents(modules map[string]model.FeedCollection) []model.Event {
	var events []model.Event
	now := clock.RealClock{}

	for _, module := range modules {
		// Generate 2-3 events per module in the 14-day range
		numEvents := rand.Intn(2) + 2 // 2 to 3 events
		for i := 0; i < numEvents; i++ {
			var startTime time.Time
			for {
				// Random day offset from today, between -7 to +7 days
				dayOffset := rand.Intn(15) - 7
				// Random time offset (start times at 9 AM, 1 PM, or 4 PM)
				timeOffsets := []time.Duration{9 * time.Hour, 13 * time.Hour, 16 * time.Hour}
				startTime = now.Now().AddDate(0, 0, dayOffset).Truncate(24 * time.Hour).Add(timeOffsets[rand.Intn(len(timeOffsets))])

				// Check if the generated day is Saturday (6) or Sunday (0), if so, regenerate the day
				weekday := startTime.Weekday()
				if weekday != time.Saturday && weekday != time.Sunday {
					break
				}
			}

			endTime := startTime.Add(2 * time.Hour) // Events last 2 hours

			// Create a formatted string for the day and week
			day := startTime.Format("2006-01-02")
			week := startTime.Format("2006-W02")

			// Create an event for this module
			event := model.Event{
				UUID:       module.UUID + "_" + startTime.Format("20060102T1504"),
				Day:        day,
				Week:       week,
				Start:      model.JSONTime(startTime),
				End:        model.JSONTime(endTime),
				Name:       module.Name,
				EventType:  "P",              // You can customize this based on module/course or other logic
				Compulsory: "V",              // Assuming all events are compulsory
				Prof:       "Prof. Dr. Test", // You can customize or randomize this as well
				Rooms:      "LI-114",         // You can customize or randomize this as well
				Notes:      "This is a test event",
				BookedAt:   now.Now().Format(time.RFC3339),
				Course:     module.Course,
				Semester:   functions.GetCurrentSemesterString(now), // Customize based on your data
			}
			events = append(events, event)
		}
	}

	return events
}

func generateTestModules() map[string]model.FeedCollection {

	return map[string]model.FeedCollection{
		"8129304z657843hf3487f69gh328467": {
			UUID:            "8129304z657843hf3487f69gh328467",
			Name:            "Big Data Management IK-M 1. FS",
			Course:          "23INM",
			UserDefinedName: "Big Data Management IK-M 1. FS",
			Reminder:        false,
		},
		"8129304z657843hf3487f69gh328468": {
			UUID:            "8129304z657843hf3487f69gh328468",
			Name:            "C133 OS Überwachungswirtschaft INM & MIM 2. FS (pf)",
			Course:          "23INM",
			UserDefinedName: "C133 OS Überwachungswirtschaft INM & MIM 2. FS (pf)",
			Reminder:        false,
		},
		"8129304z657843hf3487f69gh328469": {
			UUID:            "8129304z657843hf3487f69gh328469",
			Name:            "C230 Compilerbau INM (wpf) 2. FS",
			Course:          "23INM",
			UserDefinedName: "C230 Compilerbau INM (wpf) 2. FS",
			Reminder:        false,
		},
		"8129304z657843hf3487f69gh328470": {
			UUID:            "8129304z657843hf3487f69gh328470",
			Name:            "N873 Statistische Analyseverfahren in Data Science INM & MIM 2.FS (wpf)",
			Course:          "23INM",
			UserDefinedName: "N873 Statistische Analyseverfahren in Data Science INM & MIM 2.FS (wpf)",
			Reminder:        false,
		},
		"8129304z657843hf3487f69gh328471": {
			UUID:            "8129304z657843hf3487f69gh328471",
			Name:            "Softwareprojekt C073 MI-B & IN-B 4.FS sowie C922 Projektmanagementpraktikum IN-M 2. FS (pf)",
			Course:          "23INM",
			UserDefinedName: "Softwareprojekt C073 MI-B & IN-B 4.FS sowie C922 Projektmanagementpraktikum IN-M 2. FS (pf)",
			Reminder:        false,
		},
		"8129304z657843hf3487f69gh328472": {
			UUID:            "8129304z657843hf3487f69gh328472",
			Name:            "C803 Künstliche Intellligenz INM & MIM 2. FS (wpf)",
			Course:          "23INM",
			UserDefinedName: "C803 Künstliche Intellligenz INM & MIM 2. FS (wpf)",
			Reminder:        false,
		},
	}
}
