package config

import "os"

type Config struct {
	DatabaseURL              string
	QdrantURL                string
	OllamaURL                string
	Port                     string
	FrontendURL              string
	DownloadDir              string
	TMDBAPIKey               string
	TVDBAPIKey               string
	OpenSubtitlesAPIKey      string
	ImageDir                 string
	WorkerQueueInterval      string
	WorkerRSSSyncInterval    string
	WorkerMetadataInterval   string
	WorkerSubtitleInterval   string
	WorkerCleanupInterval    string
	WorkerHealthCheckInterval string
	WorkerDiskUsageInterval   string
	WorkerLibraryScanInterval string
}

func Load() *Config {
	databaseURL := os.Getenv("DATABASE_URL")
	if databaseURL == "" {
		databaseURL = "postgres://localhost:5432/unified_media_manager?sslmode=disable"
	}

	qdrantURL := os.Getenv("QDRANT_URL")
	if qdrantURL == "" {
		qdrantURL = "http://localhost:6333"
	}

	ollamaURL := os.Getenv("OLLAMA_URL")
	if ollamaURL == "" {
		ollamaURL = "http://localhost:11434"
	}

	frontendURL := os.Getenv("FRONTEND_URL")
	if frontendURL == "" {
		frontendURL = "http://umm.local.tophermayor.com"
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = "8084"
	}

	downloadDir := os.Getenv("DOWNLOAD_DIR")
	if downloadDir == "" {
		downloadDir = "/data/downloads"
	}

	tmdbAPIKey := os.Getenv("TMDB_API_KEY")
	tvdbAPIKey := os.Getenv("TVDB_API_KEY")
	openSubtitlesAPIKey := os.Getenv("OPENSUBTITLES_API_KEY")

	imageDir := os.Getenv("IMAGE_DIR")
	if imageDir == "" {
		imageDir = "/data/images"
	}

	workerQueueInterval := os.Getenv("WORKER_QUEUE_INTERVAL")
	if workerQueueInterval == "" {
		workerQueueInterval = "*/30 * * * * *"
	}
	workerRSSSyncInterval := os.Getenv("WORKER_RSS_SYNC_INTERVAL")
	if workerRSSSyncInterval == "" {
		workerRSSSyncInterval = "0 */15 * * * *"
	}
	workerMetadataInterval := os.Getenv("WORKER_METADATA_INTERVAL")
	if workerMetadataInterval == "" {
		workerMetadataInterval = "0 0 3 * * *"
	}
	workerSubtitleInterval := os.Getenv("WORKER_SUBTITLE_INTERVAL")
	if workerSubtitleInterval == "" {
		workerSubtitleInterval = "0 0 */2 * * *"
	}
	workerCleanupInterval := os.Getenv("WORKER_CLEANUP_INTERVAL")
	if workerCleanupInterval == "" {
		workerCleanupInterval = "0 0 4 * * *"
	}
	workerHealthCheckInterval := os.Getenv("WORKER_HEALTH_CHECK_INTERVAL")
	if workerHealthCheckInterval == "" {
		workerHealthCheckInterval = "0 */5 * * * *"
	}
	workerDiskUsageInterval := os.Getenv("WORKER_DISK_USAGE_INTERVAL")
	if workerDiskUsageInterval == "" {
		workerDiskUsageInterval = "0 0 * * * *"
	}
	workerLibraryScanInterval := os.Getenv("WORKER_LIBRARY_SCAN_INTERVAL")
	if workerLibraryScanInterval == "" {
		workerLibraryScanInterval = "0 0 5 * * *"
	}

	return &Config{
		DatabaseURL:               databaseURL,
		QdrantURL:                 qdrantURL,
		OllamaURL:                 ollamaURL,
		Port:                      port,
		FrontendURL:               frontendURL,
		DownloadDir:               downloadDir,
		TMDBAPIKey:                tmdbAPIKey,
		TVDBAPIKey:                tvdbAPIKey,
		OpenSubtitlesAPIKey:       openSubtitlesAPIKey,
		ImageDir:                  imageDir,
		WorkerQueueInterval:       workerQueueInterval,
		WorkerRSSSyncInterval:     workerRSSSyncInterval,
		WorkerMetadataInterval:    workerMetadataInterval,
		WorkerSubtitleInterval:    workerSubtitleInterval,
		WorkerCleanupInterval:     workerCleanupInterval,
		WorkerHealthCheckInterval: workerHealthCheckInterval,
		WorkerDiskUsageInterval:   workerDiskUsageInterval,
		WorkerLibraryScanInterval: workerLibraryScanInterval,
	}
}
