package main

import (
	"log"
	"net/http"
	"os"

	"github.com/joho/godotenv"
	"github.com/rede5/gohorsejobs/backend/internal/database"
	"github.com/rede5/gohorsejobs/backend/internal/router"
)

// @title           GoHorseJobs API
// @version         1.0
// @description     API for GoHorseJobs recruitment platform.
// @host            localhost:8080
// @BasePath        /
func main() {
	// Load .env file
	if err := godotenv.Load(); err != nil {
		log.Println("No .env file found or error loading it")
	}

	// Validate JWT_SECRET strength (must be at least 32 characters / 256 bits)
	jwtSecret := os.Getenv("JWT_SECRET")
	if jwtSecret == "" || len(jwtSecret) < 32 {
		log.Println("⚠️  WARNING: JWT_SECRET is empty or too short (< 32 chars). Use a strong secret in production!")
		if os.Getenv("ENV") == "production" {
			log.Fatal("FATAL: Cannot start in production without strong JWT_SECRET")
		}
	}

	database.InitDB()
	database.RunMigrations()

	handler := router.NewRouter()

	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	log.Println("Starting server on :" + port)
	if err := http.ListenAndServe(":"+port, handler); err != nil {
		log.Fatalf("Server failed to start: %v", err)
	}
}
