package bootstrap

import (
	"time"
	"tm/pkg/config"
)

// Config defines the worker application configuration
type Config struct {
	Database     config.DatabaseConfig
	Logging      config.LoggingConfig
	Notification config.NotificationConfig
	Ollama       config.OllamaConfig
	Worker       WorkerConfig
	Scraper      config.ScraperConfig
	MinIO        config.MinIOConfig
	AISummarizer AISummarizerConfig
	Queue        QueueConfig `env:"QUEUE_" envPrefix:"QUEUE_"`
	AlertMail    string      `env:"ALERT_MAIL" envDefault:"alerts@opplens.com"`
}

// QueueConfig defines the queue configuration
type QueueConfig struct {
	Enabled          bool          `env:"QUEUE_ENABLED" envDefault:"true"`
	RedisURL         string        `env:"QUEUE_REDIS_URL" envDefault:"redis://localhost:6379"`
	Mode             string        `env:"QUEUE_MODE" envDefault:"consumer"` // "producer", "consumer", or "both"
	MaxRetries       int           `env:"QUEUE_MAX_RETRIES" envDefault:"3"`
	RetryBackoff     time.Duration `env:"QUEUE_RETRY_BACKOFF" envDefault:"10s"`
	JobTimeout       time.Duration `env:"QUEUE_JOB_TIMEOUT" envDefault:"5m"`
	PoolSize         int           `env:"QUEUE_POOL_SIZE" envDefault:"10"`
	IsDLQEnabled     bool          `env:"QUEUE_DLQ_ENABLED" envDefault:"true"`
	Concurrency      int           `env:"QUEUE_CONCURRENCY" envDefault:"5"`
	ProducerBatch    int           `env:"QUEUE_PRODUCER_BATCH" envDefault:"100"`
	ProducerInterval string        `env:"QUEUE_PRODUCER_INTERVAL" envDefault:"0 */6 * * * *"` // Every 6 hours
}

type WorkerConfig struct {
	Interval                string `env:"WORKER_INTERVAL" envDefault:"* 10 * * * *"`
	NoticeProcessingLimit   int    `env:"WORKER_NOTICE_PROCESSING_LIMIT" envDefault:"0"`
	DeleteProcessedNotices  bool   `env:"WORKER_DELETE_PROCESSED_NOTICES" envDefault:"true"`
	NoticeWorkerConcurrency int    `env:"WORKER_NOTICE_CONCURRENCY" envDefault:"1"`
	NoticeFetchBatchSize    int    `env:"WORKER_NOTICE_FETCH_BATCH" envDefault:"50"`
	// NoticeBatchPause is slept after each notice batch finishes, before loading the next batch (0 = no pause).
	NoticeBatchPause time.Duration `env:"WORKER_NOTICE_BATCH_PAUSE" envDefault:"0s"`
	// NoticeFetchErrorBackoff is slept after a failed notice fetch before retrying the loop (0 = retry immediately).
	NoticeFetchErrorBackoff time.Duration `env:"WORKER_NOTICE_FETCH_ERROR_BACKOFF" envDefault:"2s"`
	TranslationInterval     string        `env:"WORKER_TRANSLATION_INTERVAL" envDefault:"0 */30 * * * *"`
	TranslationBatchSize    int           `env:"WORKER_TRANSLATION_BATCH_SIZE" envDefault:"20"`
}

// AISummarizerConfig holds configuration for the external AI summarizer service.
type AISummarizerConfig struct {
	APIBaseURL            string        `env:"AI_SUMMARIZER_API_BASE_URL" envDefault:""`
	APITimeout            time.Duration `env:"AI_SUMMARIZER_API_TIMEOUT" envDefault:"120s"`
	APIRetryCount         int           `env:"AI_SUMMARIZER_API_RETRY_COUNT" envDefault:"2"`
	APIRetryDelay         time.Duration `env:"AI_SUMMARIZER_API_RETRY_DELAY" envDefault:"3s"`
	DefaultLanguage       string        `env:"AI_SUMMARIZER_DEFAULT_LANGUAGE" envDefault:"en"`
	TranslationLanguages  string        `env:"AI_SUMMARIZER_TRANSLATION_LANGUAGES" envDefault:"en"`
	MinioEndpoint         string        `env:"AI_SUMMARIZER_MINIO_ENDPOINT" envDefault:""`
	MinioAccessKey        string        `env:"AI_SUMMARIZER_MINIO_ACCESS_KEY" envDefault:""`
	MinioSecretKey        string        `env:"AI_SUMMARIZER_MINIO_SECRET_KEY" envDefault:""`
	MinioUseSSL           bool          `env:"AI_SUMMARIZER_MINIO_USE_SSL" envDefault:"false"`
	MinioRegion           string        `env:"AI_SUMMARIZER_MINIO_REGION" envDefault:"us-east-1"`
	MinioBucket           string        `env:"AI_SUMMARIZER_MINIO_BUCKET" envDefault:"opplens-documents"`
}
