package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
	"github.com/joho/godotenv"

	"goimgApi/accounts"
	"goimgApi/configs"
	"goimgApi/images"
	"goimgApi/router"
)

// @title Go Image Manipulation API
// @version 1.0
// @description This is a sample image manipulation API using Fiber v3 and bimg.
// @host localhost:8080
// @BasePath /

// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description Type "Bearer <token>"
func main() {
	if err := godotenv.Load(); err != nil {
		log.Println("No .env file found, relying on environment variables")
	}

	if err := configs.ConnectDB(); err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}

	if err := configs.DB.AutoMigrate(&accounts.User{}, &images.Image{}); err != nil {
		log.Fatalf("Failed to run database migrations: %v", err)
	}

	if err := configs.ConnectRedis(); err != nil {
		log.Fatalf("Failed to connect to redis: %v", err)
	}

	app := fiber.New(fiber.Config{
		ErrorHandler: func(c fiber.Ctx, err error) error {
			code := fiber.StatusInternalServerError
			if e, ok := err.(*fiber.Error); ok {
				code = e.Code
			}
			return c.Status(code).JSON(fiber.Map{
				"error": err.Error(),
			})
		},
	})

	router.SetupRoutes(app)
	log.Fatal(app.Listen(":8080"))
}
