package defsecpolicies

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

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

// ListRequest struct to get a list of default security group
type ListRequest struct {
	// Filter by access group ID
	// Required: false
	AccessGroupID string `url:"access_group_id,omitempty" json:"access_group_id,omitempty"`

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

	// Number of results per page
	// Required: false
	PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`

	// Field to sort by (name, addr_count, created_at, updated_at)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`

	// Sort order (asc/desc)
	// Required: false
	SortOrder string `url:"sort_order,omitempty" json:"sort_order,omitempty"`
}

// List of default security policies
func (i DefaultSecurityPolicies) List(ctx context.Context, req ListRequest) (*SecurityPoliciesList, error) {
	res, err := i.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	policies := []SecurityPolicy{}

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

	result := SecurityPoliciesList{Policies: policies}

	return &result, nil
}

// ListRaw gets a list of all default security policies as an array of bytes
func (a DefaultSecurityPolicies) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/sdn/default_security_policy/list"

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