package segments

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

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

	// Filter by display name
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// Filter by IP version (v4 or v6)
	// Required: false
	Subnet string `url:"subnet,omitempty" json:"subnet,omitempty" validate:"omitempty,ipTypes"`

	// Filter by enabled status
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`

	// Does Core currently believe that its data is synchronized with the data in the OVN?
	// Required: false
	IsSynced interface{} `url:"is_synced,omitempty" json:"is_synced,omitempty" validate:"omitempty,isBool"`

	// Filter by update date from
	// Required: false
	UpdatedFrom string `url:"updated_from,omitempty" json:"updated_from,omitempty"`

	// Filter lby update date to
	// Required: false
	UpdatedTo string `url:"updated_to,omitempty" json:"updated_to,omitempty"`

	// Filter by create date from
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Filter lby create date to
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,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 (display_name, created_at, updated_at, subnet)
	// 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"`

	// Filter by operation status
	// Required: false
	OperationStatus string `url:"operation_status,omitempty" json:"operation_status,omitempty"`
}

// List gets list of all available segments as a ListSegment struct
func (s Segments) List(ctx context.Context, req ListRequest) (ListSegment, error) {
	res, err := s.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListSegment{}

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

	return list, nil
}

// ListRaw gets list of all available segments as an array of bytes
func (s Segments) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/sdn/segment/list"

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