package compute

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

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

// AuditsRequest struct to get audit records
type AuditsRequest struct {
	// ID of the compute
	// Required: true
	ComputeID uint64 `url:"compute_id" json:"compute_id" validate:"required"`

	// Find all audits after point in time
	// Required: false
	TimestampAT uint64 `url:"timestamp_at,omitempty" json:"timestamp_at,omitempty"`

	// Find all audits before point in time
	// Required: false
	TimestampTO uint64 `url:"timestamp_to,omitempty" json:"timestamp_to,omitempty"`

	// Find by user
	// Required: false
	User string `url:"user,omitempty" json:"user,omitempty"`

	// Find by api endpoints
	// Required: false
	Call string `url:"call,omitempty" json:"call,omitempty"`

	// Sort by one of supported fields, format ±<field>
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`

	// Page number
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Page size
	// Required: false
	Size uint64 `url:"size,omitempty" json:"size,omitempty"`

	// Find by HTTP min status code
	// Required: false
	MinStatusCode uint64 `url:"min_status_code,omitempty" json:"min_status_code,omitempty"`

	// Find by HTTP max status code
	// Required: false
	MaxStatusCode uint64 `url:"max_status_code,omitempty" json:"max_status_code,omitempty"`
}

// Audits gets audit records for the specified compute object
func (c Compute) Audits(ctx context.Context, req AuditsRequest) (*ListAudits, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/compute/audits"

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

	list := ListAudits{}

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

	return &list, nil
}
