package main

import (
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/docs"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/handlers"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
	"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and the JWT token.
func main() {
	log := logger.New(false)

	db, err := storage.OpenSession()
	if err != nil {
		log.Error("Failed to open database", "error", err)
		panic(err)
	}
	defer func() {
		err := db.Close()
		if err != nil {
			log.Error("Failed to close database", "error", err)
		}
	}()
	if err := storage.CreateTables(db); err != nil {
		log.Error("Failed to create tables", "error", err)
		panic(err)
	}

	router := gin.Default()

	// Swagger config
	docs.SwaggerInfo.BasePath = "/api/v1"
	docs.SwaggerInfo.Title = "d3m0k1d.ru API"
	docs.SwaggerInfo.Version = "1.0"
	docs.SwaggerInfo.Description = "API for d3m0k1d.ru"
	docs.SwaggerInfo.Schemes = []string{"http"}
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	handlers.Register(router, db)

	log.Info("Starting server on :8080...")
	if err := router.Run(":8080"); err != nil {
		log.Error("Error starting server", "error", err)
		panic(err)
	}
}
