package tasks

import (
	"context"
	"encoding/json"
	"net/http"

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

// GetRequest struct to get background API task status and result
type GetRequest struct {
	// ID of audit GUID
	// Required: true
	AuditID string `url:"auditId" json:"auditId" validate:"required"`
}

// Get gets background API task status and result as a RecordTask struct
func (t Tasks) Get(ctx context.Context, req GetRequest) (*RecordTask, error) {
	res, err := t.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	item := RecordTask{}

	err = json.Unmarshal(res, &item)
	if err != nil {
		return nil, err
	}

	return &item, nil
}

// GetRaw gets background API task status and result as an array of bytes
func (t Tasks) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/tasks/get"

	res, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
	return res, err
}
