package audit

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

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

// GetRequest struct to get information about account
type GetRequest struct {
	// Audit GUID
	// Required: true
	AuditGuid string `url:"audit_guid" json:"audit_guid" validate:"required"`
}

// Get gets information about audit as a RecordAudit struct
func (a Audit) Get(ctx context.Context, req GetRequest) (*RecordAudit, error) {
	res, err := a.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordAudit{}

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

	return &info, nil
}

// GetRaw gets information about audit as an array of bytes
func (a Audit) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/audit/get"

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