package utils

import (
	"errors"
	authdomain "legalgo-BE-go/internal/domain/auth"
	"net/http"
	"strings"
)

func GetTokenDetail(r *http.Request) (authdomain.AuthToken, error) {
	authHeader := r.Header.Get("Authorization")

	var data authdomain.AuthToken

	if authHeader == "" {
		return data, errors.New("unauthorized")
	}

	if !strings.HasPrefix(authHeader, "Bearer") {
		return data, errors.New("invalid token")
	}

	token := strings.Split(authHeader, " ")
	if len(token) < 2 {
		return data, errors.New("invalid token")
	}
	data, err := DestructToken(token[1])

	if err != nil {
		return data, err
	}

	return data, nil
}
