package main

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

	"mkiso-server/internal/config"
	"mkiso-server/internal/repo"
	"mkiso-server/internal/route"
	"mkiso-server/internal/service"
)

func main() {
	slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	})))

	// Load configuration
	cfgPath := ""
	if len(os.Args) > 1 {
		cfgPath = os.Args[1]
	}

	cfg, err := config.Load(cfgPath)
	if err != nil {
		slog.Error("failed to load config", "error", err)
		os.Exit(1)
	}

	// Print config summary
	slog.Info("configuration loaded",
		"port", cfg.Server.Port,
		"auth_enabled", cfg.Auth.Enabled,
		"pacs", fmt.Sprintf("%s@%s:%d", cfg.PACS.AETitle, cfg.PACS.Host, cfg.PACS.Port),
		"our_ae", cfg.OurAE.AETitle,
		"cd_publisher", fmt.Sprintf("%s:%d", cfg.CDPublisher.Host, cfg.CDPublisher.Port),
	)

	// Initialize repositories
	patientRepo := repo.NewPatientRepo(
		cfg.PatientAPI.BaseURL,
		cfg.PatientAPI.Endpoint,
		cfg.PatientAPI.AuthHeader,
		cfg.PatientAPI.AuthToken,
		cfg.PatientAPI.Timeout,
		cfg.PatientAPI.Retry,
		cfg.PatientAPI.RetryBackoff,
	)

	// Initialize services
	dicomSvc := service.NewDicomService(cfg)
	isoSvc := service.NewISOService(cfg, dicomSvc, patientRepo)
	relaySvc := service.NewRelayService(cfg, dicomSvc, patientRepo)

	// Build handler
	var h http.Handler
	if cfg.Auth.Enabled {
		slog.Info("authentication enabled")
		h = route.SetupSecure(cfg, isoSvc, relaySvc)
	} else {
		slog.Info("authentication disabled (Stage 1)")
		h = route.Setup(cfg, isoSvc, relaySvc)
	}

	// Start server
	addr := fmt.Sprintf(":%d", cfg.Server.Port)
	server := &http.Server{
		Addr:         addr,
		Handler:      h,
		ReadTimeout:  cfg.Server.ReadTimeout,
		WriteTimeout: cfg.Server.WriteTimeout,
	}

	// Graceful shutdown
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		slog.Info("server starting", "addr", addr)
		if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			slog.Error("server error", "error", err)
			os.Exit(1)
		}
	}()

	// Wait for shutdown signal
	sig := <-quit
	slog.Info("shutting down server", "signal", sig)

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	if err := server.Shutdown(ctx); err != nil {
		slog.Error("server forced to shutdown", "error", err)
		os.Exit(1)
	}

	slog.Info("server stopped")
}

