package main

import (
	"context"
	"log"

	"photum-backend/docs"
	"photum-backend/internal/agenda"
	"photum-backend/internal/anos_formaturas"
	"photum-backend/internal/auth"
	"photum-backend/internal/availability"
	"photum-backend/internal/cadastro_fot"
	"photum-backend/internal/codigos"
	"photum-backend/internal/config"
	"photum-backend/internal/cursos"
	"photum-backend/internal/db"
	"photum-backend/internal/empresas"
	"photum-backend/internal/escalas"
	"photum-backend/internal/funcoes"
	"photum-backend/internal/logistica"
	"photum-backend/internal/profissionais"
	"photum-backend/internal/storage"
	"photum-backend/internal/tipos_eventos"
	"photum-backend/internal/tipos_servicos"
	"strings"

	"github.com/gin-contrib/cors"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	// "photum-backend/docs" is already imported above
)

// @title           Photum Backend API
// @version         1.0
// @description     Backend authentication service for Photum.
// @termsOfService  http://swagger.io/terms/

// @contact.name    API Support
// @contact.url     http://www.swagger.io/support
// @contact.email   support@swagger.io

// @license.name    Apache 2.0
// @license.url     http://www.apache.org/licenses/LICENSE-2.0.html

// @host            localhost:8080
// @BasePath        /
// @tag.name        auth
// @tag.description Authentication related operations
// @tag.name        admin
// @tag.description Administration operations

// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
	cfg := config.LoadConfig()
	log.Printf("Loaded DSN: %s", cfg.DBDsn)

	queries, pool := db.Connect(cfg)
	defer pool.Close()

	// Run Migrations
	db.Migrate(pool)

	// Initialize services
	profissionaisService := profissionais.NewService(queries)
	authService := auth.NewService(queries, profissionaisService, cfg)
	funcoesService := funcoes.NewService(queries)
	cursosService := cursos.NewService(queries)
	empresasService := empresas.NewService(queries)
	anosFormaturasService := anos_formaturas.NewService(queries)
	tiposServicosService := tipos_servicos.NewService(queries)
	tiposEventosService := tipos_eventos.NewService(queries)
	cadastroFotService := cadastro_fot.NewService(queries)
	agendaService := agenda.NewService(queries)
	availabilityService := availability.NewService(queries)
	s3Service := storage.NewS3Service(cfg)

	// Seed Demo Users
	if err := authService.EnsureDemoUsers(context.Background()); err != nil {
		log.Printf("Failed to seed demo users: %v", err)
	}

	// Initialize handlers
	authHandler := auth.NewHandler(authService, s3Service)
	profissionaisHandler := profissionais.NewHandler(profissionaisService)
	funcoesHandler := funcoes.NewHandler(funcoesService)
	cursosHandler := cursos.NewHandler(cursosService)
	empresasHandler := empresas.NewHandler(empresasService)
	anosFormaturasHandler := anos_formaturas.NewHandler(anosFormaturasService)
	tiposServicosHandler := tipos_servicos.NewHandler(tiposServicosService)
	tiposEventosHandler := tipos_eventos.NewHandler(tiposEventosService)
	cadastroFotHandler := cadastro_fot.NewHandler(cadastroFotService)
	agendaHandler := agenda.NewHandler(agendaService)
	availabilityHandler := availability.NewHandler(availabilityService)
	escalasHandler := escalas.NewHandler(escalas.NewService(queries))
	logisticaHandler := logistica.NewHandler(logistica.NewService(queries))
	codigosHandler := codigos.NewHandler(codigos.NewService(queries))

	r := gin.Default()

	// CORS Middleware
	configCors := cors.DefaultConfig()
	if cfg.CorsAllowedOrigins == "*" {
		configCors.AllowAllOrigins = true
	} else {
		configCors.AllowOrigins = strings.Split(cfg.CorsAllowedOrigins, ",")
	}
	configCors.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}
	r.Use(cors.New(configCors))

	// Swagger
	// Dynamically update Swagger Info
	docs.SwaggerInfo.Host = cfg.SwaggerHost
	if cfg.AppEnv == "production" {
		docs.SwaggerInfo.Schemes = []string{"https", "http"}
	} else {
		docs.SwaggerInfo.Schemes = []string{"http", "https"}
	}

	// Swagger UI
	url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler,
		ginSwagger.PersistAuthorization(true),
		ginSwagger.DeepLinking(true),
		url,
	))

	// Public Routes
	authGroup := r.Group("/auth")
	{
		authGroup.POST("/register", authHandler.Register)
		authGroup.POST("/login", authHandler.Login)
		authGroup.POST("/refresh", authHandler.Refresh)
		authGroup.POST("/logout", authHandler.Logout)
		authGroup.POST("/upload-url", authHandler.GetUploadURL)
	}

	// Public API Routes (Data Lists)
	r.GET("/api/funcoes", funcoesHandler.List)
	r.GET("/api/cursos", cursosHandler.List)
	r.GET("/api/empresas", empresasHandler.List)
	r.GET("/api/anos-formaturas", anosFormaturasHandler.List)
	r.GET("/api/tipos-servicos", tiposServicosHandler.List)
	r.GET("/api/tipos-eventos", tiposEventosHandler.List)
	r.GET("/api/tipos-eventos/:id/precos", tiposEventosHandler.ListPrices)
	r.GET("/api/public/codigos-acesso/verificar", codigosHandler.Verify)

	// Protected Routes
	api := r.Group("/api")
	api.Use(auth.AuthMiddleware(cfg))
	{
		api.GET("/me", authHandler.Me)

		profGroup := api.Group("/profissionais")
		{
			profGroup.POST("", profissionaisHandler.Create)
			profGroup.GET("", profissionaisHandler.List)
			profGroup.GET("/:id", profissionaisHandler.Get)
			profGroup.PUT("/:id", profissionaisHandler.Update)
			profGroup.DELETE("/:id", profissionaisHandler.Delete)
		}

		funcoesGroup := api.Group("/funcoes")
		{
			funcoesGroup.POST("", funcoesHandler.Create)
			funcoesGroup.PUT("/:id", funcoesHandler.Update)
			funcoesGroup.DELETE("/:id", funcoesHandler.Delete)
		}

		// protected CRUD (create/update/delete)
		api.POST("/cursos", cursosHandler.Create)
		api.PUT("/cursos/:id", cursosHandler.Update)
		api.DELETE("/cursos/:id", cursosHandler.Delete)

		api.POST("/empresas", empresasHandler.Create)
		api.PUT("/empresas/:id", empresasHandler.Update)
		api.DELETE("/empresas/:id", empresasHandler.Delete)

		api.POST("/anos-formaturas", anosFormaturasHandler.Create)
		api.PUT("/anos-formaturas/:id", anosFormaturasHandler.Update)
		api.DELETE("/anos-formaturas/:id", anosFormaturasHandler.Delete)

		api.POST("/tipos-servicos", tiposServicosHandler.Create)
		api.PUT("/tipos-servicos/:id", tiposServicosHandler.Update)
		api.DELETE("/tipos-servicos/:id", tiposServicosHandler.Delete)

		api.POST("/tipos-eventos", tiposEventosHandler.Create)
		api.PUT("/tipos-eventos/:id", tiposEventosHandler.Update)
		api.DELETE("/tipos-eventos/:id", tiposEventosHandler.Delete)
		api.POST("/tipos-eventos/precos", tiposEventosHandler.SetPrice)

		api.GET("/cadastro-fot", cadastroFotHandler.List)
		api.POST("/cadastro-fot", cadastroFotHandler.Create)
		api.GET("/cadastro-fot/:id", cadastroFotHandler.Get)
		api.PUT("/cadastro-fot/:id", cadastroFotHandler.Update)
		api.DELETE("/cadastro-fot/:id", cadastroFotHandler.Delete)

		api.GET("/agenda", agendaHandler.List)
		api.POST("/agenda", agendaHandler.Create)
		api.GET("/agenda/:id", agendaHandler.Get)
		api.PUT("/agenda/:id", agendaHandler.Update)
		api.DELETE("/agenda/:id", agendaHandler.Delete)
		api.POST("/agenda/:id/professionals", agendaHandler.AssignProfessional)
		api.DELETE("/agenda/:id/professionals/:profId", agendaHandler.RemoveProfessional)
		api.GET("/agenda/:id/professionals", agendaHandler.GetProfessionals)
		api.PATCH("/agenda/:id/professionals/:profId/status", agendaHandler.UpdateAssignmentStatus)
		api.PATCH("/agenda/:id/professionals/:profId/position", agendaHandler.UpdateAssignmentPosition)
		api.GET("/agenda/:id/available", agendaHandler.ListAvailableProfessionals)
		api.PATCH("/agenda/:id/status", agendaHandler.UpdateStatus)

		api.POST("/availability", availabilityHandler.SetAvailability)
		api.GET("/availability", availabilityHandler.ListAvailability)

		// Escalas Routes
		api.POST("/escalas", escalasHandler.Create)
		api.GET("/escalas", escalasHandler.ListByAgenda)
		api.DELETE("/escalas/:id", escalasHandler.Delete)
		api.PUT("/escalas/:id", escalasHandler.Update)

		// Logistics Routes
		logisticaGroup := api.Group("/logistica")
		{
			logisticaGroup.POST("/carros", logisticaHandler.CreateCarro)
			logisticaGroup.GET("/carros", logisticaHandler.ListCarros)
			logisticaGroup.DELETE("/carros/:id", logisticaHandler.DeleteCarro)
			logisticaGroup.PUT("/carros/:id", logisticaHandler.UpdateCarro)

			logisticaGroup.POST("/carros/:id/passageiros", logisticaHandler.AddPassenger)
			logisticaGroup.DELETE("/carros/:id/passageiros/:profID", logisticaHandler.RemovePassenger)
			logisticaGroup.GET("/carros/:id/passageiros", logisticaHandler.ListPassengers)
		}

		codigosGroup := api.Group("/codigos-acesso")
		{
			codigosGroup.POST("", codigosHandler.Create)
			codigosGroup.GET("", codigosHandler.List)
			codigosGroup.DELETE("/:id", codigosHandler.Delete)
		}

		admin := api.Group("/admin")
		{
			admin.GET("/users", authHandler.ListUsers)
			admin.GET("/users/pending", authHandler.ListPending)
			admin.GET("/users/:id", authHandler.GetUser)
			admin.PATCH("/users/:id/approve", authHandler.Approve)
			admin.POST("/users", authHandler.AdminCreateUser)
			admin.PATCH("/users/:id/role", authHandler.UpdateRole)
			admin.DELETE("/users/:id", authHandler.DeleteUser)
		}
	}

	log.Printf("Swagger Host Configured: %s", cfg.SwaggerHost)
	log.Printf("Server running on port %s", cfg.AppPort)
	r.Run(":" + cfg.AppPort)
}
