package segments

import (
	"context"

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

func utilitySegmentListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*segments.ListSegment, error) {
	c := m.(*controller.ControllerCfg)
	req := segments.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 enabled, ok := d.GetOk("enabled"); ok {
		req.Enabled = enabled.(bool)
	}
	if isSynced, ok := d.GetOk("is_synced"); ok {
		req.IsSynced = isSynced.(bool)
	}
	if displayName, ok := d.GetOk("display_name"); ok {
		req.DisplayName = displayName.(string)
	}
	if subnet, ok := d.GetOk("subnet"); ok {
		req.Subnet = subnet.(string)
	}
	if accessGroupID, ok := d.GetOk("access_group_id"); ok {
		req.AccessGroupID = accessGroupID.(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)
	}
	if operationStatus, ok := d.GetOk("operation_status"); ok {
		req.OperationStatus = operationStatus.(string)
	}

	log.Debugf("utilitySegmentListCheckPresence")
	segmentList, err := c.SDN().Segments().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return &segmentList, nil
}
