package middlewares

import (
	"strings"

	"github.com/gofiber/fiber/v3"
)

func (f *Middlewares) AuthAdmin(ctx fiber.Ctx) error {
	if !strings.HasPrefix(ctx.Path(), "/v1/admin") {
		return ctx.Next()
	}

	if ctx.Path() == "/v1/admin/auth" {
		return ctx.Next()
	}

	token := ctx.Get("Authorization")
	if token == "" {
		token = ctx.Query("token")
		if token == "" {
			return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
		}
	}
	jwt, err := f.jwt.Parse(token)
	if err != nil {
		return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
	}
	if jwt.UserID != -20140202 {
		return ctx.Status(fiber.StatusForbidden).SendString("Forbidden")
	}

	return ctx.Next()
}
