package user

import (
	"context"
	"net/http"

	"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)

// AuthenticateRequest struct to authenticate user.
type AuthenticateRequest struct {
	// Username
	// Required: true
	Username string `url:"username" json:"username" validate:"required"`

	// Password
	// Required: true
	Password string `url:"password" json:"password" validate:"required"`
}

// Authenticate evaluates the provided username and password and returns a session key.
// The session key can be used for doing api requests. E.g this is the authkey parameter in every actor request.
// A session key is only vallid for a limited time.
func (u User) Authenticate(ctx context.Context, req AuthenticateRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/user/authenticate"

	res, err := u.client.DecortApiCall(ctx, http.MethodPost, url, req)
	if err != nil {
		return "", err
	}

	return string(res), nil
}
