package main

import (
	"context"
	"log"
	"time"

	"trip-planner/internal/cache"
	"trip-planner/internal/yandex"
)

// stationStatusKey returns the Redis key for station status.
func stationStatusKey(id string) *cache.CacheKey {
	return &cache.CacheKey{
		Kind: "station",
		Code: id,
	}
}

func main() {
	ctx := context.Background()

	// Initialize Redis cache
	redisClient := cache.NewRedisCache(&cache.RedisConfig{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	// Initialize Yandex client
	yandexClient := yandex.NewClient("test-key")

	// Initialize station monitors for monitored stations
	monitors := []*cron.StationMonitor{
		{
			ID:     "station-moscow-kiev",
			Yandex: yandexClient,
			Cache:  redisClient,
		},
		{
			ID:     "station-petersburg-moscow",
			Yandex: yandexClient,
			Cache:  redisClient,
		},
	}

	// Process all stations - this is the main cron job function
	if err := cron.ProcessAllStations(ctx, monitors); err != nil {
		log.Printf("ERROR: failed to process stations: %v", err)
	}

	// Log the status of all monitored stations
	for _, monitor := range monitors {
		statusKey := stationStatusKey(monitor.ID)
		statusData, err := monitor.Cache.Get(ctx, statusKey)
		if err == nil && statusData != nil {
			log.Printf("INFO: station %s status: %s", monitor.ID, string(statusData))
		}
	}

	log.Println("Cron job completed")
}