package logicalports

import (
	"context"

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

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

	if accessGroupID, ok := d.GetOk("access_group_id"); ok {
		req.AccessGroupID = accessGroupID.(string)
	}
	if segmentID, ok := d.GetOk("segment_id"); ok {
		req.SegmentID = segmentID.(string)
	}
	if segmentDisplayName, ok := d.GetOk("segment_display_name"); ok {
		req.SegmentDisplayName = segmentDisplayName.(string)
	}
	if externalNetworkID, ok := d.GetOk("external_network_id"); ok {
		req.ExternalNetworkID = externalNetworkID.(string)
	}
	if uniqueID, ok := d.GetOk("unique_identifier"); ok {
		req.UniqueIdentifier = uniqueID.(string)
	}
	if displayName, ok := d.GetOk("display_name"); ok {
		req.DisplayName = displayName.(string)
	}
	if adapterMAC, ok := d.GetOk("adapter_mac"); ok {
		req.AdapterMAC = adapterMAC.(string)
	}
	if hypervisor, ok := d.GetOk("hypervisor"); ok {
		req.Hypervisor = hypervisor.(string)
	}
	if hypervisorDisplayName, ok := d.GetOk("hypervisor_display_name"); ok {
		req.HypervisorDisplayName = hypervisorDisplayName.(string)
	}
	if liveMigrationTargetHV, ok := d.GetOk("live_migration_target_hv"); ok {
		req.LiveMigrationTargetHv = liveMigrationTargetHV.(string)
	}
	if portSecurity, ok := d.GetOk("port_security"); ok {
		req.PortSecurity = portSecurity.(bool)
	}
	if addressDetection, ok := d.GetOk("address_detection"); ok {
		req.AddressDetection = addressDetection.(bool)
	}
	if enabled, ok := d.GetOk("enabled"); ok {
		req.Enabled = enabled.(bool)
	}
	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 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 operationStatus, ok := d.GetOk("operation_status"); ok {
		req.OperationStatus = operationStatus.(string)
	}
	if hypervisorStatus, ok := d.GetOk("hypervisor_status"); ok {
		req.HypervisorStatus = hypervisorStatus.(string)
	}

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

	return logicalPortList, nil
}
