package accessgroup

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/acsgroups"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilityAccessGroupListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*acsgroups.AccessGroupList, error) {
	c := m.(*controller.ControllerCfg)
	req := acsgroups.ListGroupsRequest{}

	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 deleted, ok := d.GetOk("deleted"); ok {
		req.Deleted = deleted.(bool)
	}
	if displayName, ok := d.GetOk("display_name"); ok {
		req.DisplayName = displayName.(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 ownerDisplayName, ok := d.GetOk("owner_display_name"); ok {
		req.OwnerDisplayName = ownerDisplayName.(string)
	}

	log.Debugf("utilityAccessGroupListCheckPresence")
	accessGroupList, err := c.SDN().AccessGroups().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return accessGroupList, nil
}
