package external

import (
	"errors"
	"fmt"
	"log/slog"
	"net/http"
	"time"
)

func (s *Manager) ValidateSession(sessionToken string) (bool, error) {
	path := "/api/v1/auth/session/validate"

	auth, ok := s.config.External[QApixService]
	if !ok {
		err := errors.New("error validating auth to qapix service at ValidateSession")
		slog.Error(err.Error())

		return false, err
	}

	options := RequestOptions{
		Method:        http.MethodGet,
		Path:          path,
		Body:          nil,
		Retries:       3,
		Timeout:       sTimeout * time.Second,
		CacheDuration: time.Second * 10,
		Auth:          &auth,
		SessionToken:  sessionToken,
	}

	res, err := s.sendRequestToExternal(options)
	if err != nil || res == nil {
		err := fmt.Errorf("error making ValidateSession request to qapix server. error: %w", err)
		slog.Error(err.Error())

		return false, err
	}

	return true, nil
}
