package hypervisors

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn/hypervisors"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilityHypervisorListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (hypervisors.HypervisorsList, error) {
	c := m.(*controller.ControllerCfg)
	req := hypervisors.ListRequest{}

	if page, ok := d.GetOk("page"); ok {
		req.Page = uint64(page.(int))
	}

	if perPage, ok := d.GetOk("per_page"); ok {
		req.PerPage = uint64(perPage.(int))
	}

	if sortBy, ok := d.GetOk("sort_by"); ok {
		req.SortBy = sortBy.(string)
	}

	if sortOrder, ok := d.GetOk("sort_order"); ok {
		req.SortOrder = sortOrder.(string)
	}

	if portInfo, ok := d.GetOk("port_info"); ok {
		req.PortInfo = portInfo.(string)
	}

	if hostname, ok := d.GetOk("hostname"); ok {
		req.Hostname = hostname.(string)
	}

	if displayName, ok := d.GetOk("display_name"); ok {
		req.DisplayName = displayName.(string)
	}

	if ip, ok := d.GetOk("ip"); ok {
		req.IP = ip.(string)
	}

	if createdFrom, ok := d.GetOk("created_from"); ok {
		req.CreatedFrom = createdFrom.(string)
	}

	if createdTo, ok := d.GetOk("created_to"); ok {
		req.CreatedTo = createdTo.(string)
	}

	if updatedFrom, ok := d.GetOk("updated_from"); ok {
		req.UpdatedFrom = updatedFrom.(string)
	}

	if updatedTo, ok := d.GetOk("updated_to"); ok {
		req.UpdatedTo = updatedTo.(string)
	}

	hypervisorList, err := c.SDN().Hypervisors().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return hypervisorList, nil
}
