package utils

import (
	"crypto/rand"
	"encoding/json"
	"math/big"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog/log"
)

func GetXAuhorizationHeader(c *fiber.Ctx) string {
	return c.GetReqHeaders()[HeaderXAuthorization]
}

func GetXApiKeyHeader(c *fiber.Ctx) string {
	return c.GetReqHeaders()[HeaderXApiKey]
}

func MarshalJson(v any) string {
	json, err := json.Marshal(v)

	if err != nil {
		log.Error().Msgf("Failed to marshal json %s", err)
		return ""
	}

	return string(json)
}

func GetSessionExpiresAtTime() time.Time {
	return time.Now().Add(time.Second * SessionExpiresAtTime)
}

func IsPasswordLengthValid(password string) bool {
	lenPassword := len(password)

	if lenPassword < MinPassword || lenPassword > MaxPassword {
		log.Error().Msg("Password length not valid")
		return false
	}

	return true
}

func GenerateSession() (string, error) {
	var letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

	r := make([]byte, 36)

	for i := 0; i < 36; i++ {
		num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))

		if err != nil {
			log.Error().Msgf("Failed to session: %v", err)
			return "", err
		}

		if i == 8 || i == 13 || i == 18 || i == 23 {
			r[i] = 45
		} else {
			r[i] = letters[num.Int64()]
		}
	}

	return string(r), nil
}
