package audit

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

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

// ListRequest struct to give list of account audits
type ListRequest struct {

	// Find all audits after point in time (unixtime)
	// Required: false
	TimestampAt uint64 `url:"timestamp_at,omitempty" json:"timestamp_at,omitempty"`

	// Find all audits before point in time (unixtime)
	// Required: false
	TimestampTo uint64 `url:"timestamp_to,omitempty" json:"timestamp_to,omitempty"`

	// Find by user (Mongo RegExp supported)
	// Required: false
	User string `url:"user,omitempty" json:"user,omitempty"`

	// Find by api endpoint (Mongo RegExp supported)
	// Required: false
	Call string `url:"call,omitempty" json:"call,omitempty"`

	// Find by request id
	// Required: false
	RequestID string `url:"request_id,omitempty" json:"request_id,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"`

	// Sort by one of supported fields, format +|-(field)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty" validate:"omitempty,sortBy"`

	// 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 resource group id
	// Required: false
	RGID uint64 `url:"resgroup_id,omitempty" json:"resgroup_id,omitempty"`

	// Find by compute id
	// Required: false
	ComputeID uint64 `url:"compute_id,omitempty" json:"compute_id,omitempty"`

	// Find by account id
	// Required: false
	AccountID uint64 `url:"account_id,omitempty" json:"account_id,omitempty"`

	// Find by vins id
	// Required: false
	VINSID uint64 `url:"vins_id,omitempty" json:"vins_id,omitempty"`

	// Find by service id
	// Required: false
	ServiceID uint64 `url:"service_id,omitempty" json:"service_id,omitempty"`

	// Find by k8s id
	// Required: false
	K8SID uint64 `url:"k8s_id,omitempty" json:"k8s_id,omitempty"`

	// Find by flipgroup id
	// Required: false
	FLIPGroupID uint64 `url:"flipgroup_id,omitempty" json:"flipgroup_id,omitempty"`

	// Find by load balancer id
	// Required: false
	LBID uint64 `url:"lb_id,omitempty" json:"lb_id,omitempty"`

	// Find by sep id
	// Required: false
	SEPID uint64 `url:"sep_id,omitempty" json:"sep_id,omitempty"`

	// Exclude audit lines from response
	// Required: false
	ExcludeAuditLines bool `url:"exclude_audit_lines,omitempty" json:"exclude_audit_lines,omitempty"`

	// Public Session ID
	PublicSessionID string `json:"public_session_id"`
}

// List gets audit records for the specified account object
func (a Audit) List(ctx context.Context, req ListRequest) (*ListAudits, error) {

	res, err := a.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListAudits{}

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

	return &list, nil
}

// ListRaw gets list of audit records an array of bytes
func (a Audit) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

	if err := validators.ValidateRequest(req); err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/audit/list"

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