package main

import (
	"fmt"
	"net/http"
	"os"

	"go.b0esche.cloud/backend/internal/audit"
	"go.b0esche.cloud/backend/internal/auth"
	"go.b0esche.cloud/backend/internal/config"
	"go.b0esche.cloud/backend/internal/database"
	httpsrv "go.b0esche.cloud/backend/internal/http"
	"go.b0esche.cloud/backend/pkg/jwt"
)

func main() {
	cfg := config.Load()

	dbConn, err := database.Connect(cfg)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Database connection error: %v\n", err)
		os.Exit(1)
	}
	db := database.New(dbConn)

	jwtManager := jwt.NewManager(cfg.JWTSecret)

	authService := auth.NewService(db)

	auditLogger := audit.NewLogger(db)

	srv := httpsrv.New(cfg, db, jwtManager, authService, auditLogger)

	fmt.Printf("Starting server on %s\n", cfg.ServerAddr)
	if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		fmt.Fprintf(os.Stderr, "Server error: %v\n", err)
		os.Exit(1)
	}
}
