package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"scheduler-backend/internal/executor"
	"scheduler-backend/internal/jobdef"
	"scheduler-backend/internal/scheduler"
	storemongo "scheduler-backend/internal/store/mongo"
	"scheduler-backend/pkg/config"
	"scheduler-backend/pkg/log"
	redisclient "scheduler-backend/pkg/redis"

	"github.com/go-co-op/gocron/v2"
)

func main() {
	cfg := config.Load()
	logger := log.New()
	registry := jobdef.NewRegistry(
		jobdef.SampleHandler{},
		jobdef.URLRewriteHandler{},
		jobdef.S3MigrateHandler{},
	)
	rootCtx := context.Background()
	databases, err := storemongo.Connect(rootCtx, cfg)
	if err != nil {
		logger.Error("scheduler worker mongo connect failed", "error", err)
		os.Exit(1)
	}
	redisConn, err := redisclient.Connect(rootCtx, cfg)
	if err != nil {
		logger.Error("scheduler worker redis connect failed", "error", err)
		os.Exit(1)
	}

	jobConfigStore := storemongo.NewJobConfigStore(databases.MetaDB)
	if err := jobdef.SyncJobConfigs(rootCtx, "job-config-list", jobConfigStore, logger); err != nil {
		logger.Error("sync job configs failed", "error", err)
		os.Exit(1)
	}

	runtime := jobdef.Runtime{
		Config:     cfg,
		Logger:     logger,
		MetaDB:     databases.MetaDB,
		BusinessDB: databases.BusinessDB,
		Redis:      redisConn,
	}
	execSvc := executor.NewService(registry, runtime)
	g, err := gocron.NewScheduler()
	if err != nil {
		logger.Error("scheduler worker init scheduler failed", "error", err)
		os.Exit(1)
	}
	svc := scheduler.NewService(
		g,
		jobConfigStore,
		storemongo.NewJobExecutionStore(databases.MetaDB),
		execSvc,
		runtime,
	)

	healthServer := &http.Server{
		Addr:              fmt.Sprintf(":%d", cfg.WorkerHTTPPort),
		ReadHeaderTimeout: 5 * time.Second,
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			switch r.URL.Path {
			case "/healthz", "/readyz":
				w.WriteHeader(http.StatusOK)
				_, _ = w.Write([]byte("ok"))
			default:
				w.WriteHeader(http.StatusNotFound)
			}
		}),
	}

	go func() {
		logger.Info("scheduler worker health server starting", "addr", healthServer.Addr)
		if err := healthServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			logger.Error("scheduler worker health server stopped", "error", err)
		}
	}()

	logger.Info("scheduler worker starting", "db", cfg.SchedulerMongoDatabase, "healthAddr", healthServer.Addr)
	if err := svc.RegisterEnabledJobs(rootCtx); err != nil {
		logger.Error("scheduler worker register jobs failed", "error", err)
		os.Exit(1)
	}

	stop := make(chan os.Signal, 1)
	signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
	<-stop

	if err := svc.Shutdown(); err != nil {
		logger.Error("scheduler worker shutdown failed", "error", err)
	}
	shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := healthServer.Shutdown(shutdownCtx); err != nil {
		logger.Error("scheduler worker health server shutdown failed", "error", err)
	}
	if err := redisConn.Close(); err != nil {
		logger.Error("scheduler worker redis disconnect failed", "error", err)
	}
	if err := databases.Client.Disconnect(context.Background()); err != nil {
		logger.Error("scheduler worker mongo disconnect failed", "error", err)
	}
}
