package stpolicy

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

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

type ListRequest struct {
	// Page number
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

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

	// Search by storage policy ID
	// Required: false
	ByID uint64 `url:"by_id,omitempty" json:"by_id,omitempty"`

	// Search by storage policy name
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Search by storage policy status
	// Required: false
	Status string `url:"status,omitempty" json:"status,omitempty"`

	// Search by storage policy desc
	// Required: false
	Desc string `url:"desc,omitempty" json:"desc,omitempty"`

	// Search by storage policy iops limit
	// Required: false
	LimitIOPS uint64 `url:"limit_iops,omitempty" json:"limit_iops,omitempty"`

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

	// ID of account ID
	// Required: false
	AccountID uint64 `url:"account_id,omitempty" json:"account_id,omitempty"`

	// Search by resgroup id
	// Required: false
	ResgroupID uint64 `url:"resgroup_id,omitempty" json:"resgroup_id,omitempty"`

	// Search by sep id
	// Required: false
	SepID uint64 `url:"sep_id,omitempty" json:"sep_id,omitempty"`

	// Search by pool name
	// Required: false
	PoolName string `url:"pool_name,omitempty" json:"pool_name,omitempty"`

	// Filter SEP's by tech status (ENABLED, DISABLED)
	// Required: false
	SepTechStatus string `url:"sep_tech_status,omitempty" json:"sep_tech_status,omitempty" validate:"omitempty,sepTechStatus"`
}

// List gets list of storage policies as a ListStoragePolicies struct
func (sp StPolicy) List(ctx context.Context, req ListRequest) (*ListStoragePolicies, error) {

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

	list := ListStoragePolicies{}

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

	return &list, nil
}

// ListRaw gets list of storage policies as an array of bytes
func (sp StPolicy) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/cloudbroker/storage_policy/list"

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