package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"
	"time"
	"tm/cmd/worker/bootstrap"
)

func main() {
	// Load configuration
	config, err := bootstrap.InitConfig()
	if err != nil {
		panic(err)
	}

	// Initialize logger
	appLogger := bootstrap.InitLogger(config.Logging)
	appLogger.Info("Starting worker application", map[string]interface{}{
		"version": "1.0.0",
	})

	// Initialize MongoDB connection
	mongoManager := bootstrap.InitMongoDB(config.Database.MongoDB, appLogger)
	defer func() {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		if err := mongoManager.Close(ctx); err != nil {
			appLogger.Error("Failed to close MongoDB connection", map[string]interface{}{
				"error": err.Error(),
			})
		}
	}()

	// Initialize notification service
	notificationService := bootstrap.InitNotificationService(config.Notification, appLogger)

	// Initialize MinIO service
	minioService := bootstrap.InitMinIOService(config.MinIO, appLogger)
	if minioService != nil {
		appLogger.Info("MinIO service initialized successfully", map[string]interface{}{})
	}

	// Initialize AI summarizer client (translation + document summarization workers)
	aiSummarizerClient := bootstrap.InitAISummarizerClient(config.AISummarizer, appLogger)
	aiSummarizerStorage := bootstrap.InitAISummarizerStorage(config.AISummarizer, appLogger)

	// Initialize Worker
	scheduler := bootstrap.InitWorker(*config, mongoManager, appLogger, notificationService, minioService, aiSummarizerClient, aiSummarizerStorage)

	// Set up signal handling for graceful shutdown
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	// Wait for shutdown signal
	<-signalChan
	appLogger.Info("Received shutdown signal, stopping scheduler...", map[string]interface{}{})

	// Stop scheduler gracefully
	if scheduler != nil {
		scheduler.Stop()
	}

	appLogger.Info("Worker application stopped", map[string]interface{}{})
}
