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 utilityAccessGroupUserListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*acsgroups.UsersList, error) {
	c := m.(*controller.ControllerCfg)
	req := acsgroups.UsersListRequest{}

	req.AccessGroupID = d.Get("access_group_id").(string)

	if globalRole, ok := d.GetOk("global_role"); ok {
		req.GlobalRole = globalRole.(string)
	}
	if accessGroupRole, ok := d.GetOk("access_group_role"); ok {
		req.AccessGroupRole = accessGroupRole.(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 login, ok := d.GetOk("login"); ok {
		req.Login = login.(string)
	}
	if createdBy, ok := d.GetOk("created_by"); ok {
		req.CreatedBy = createdBy.(string)
	}
	if updatedBy, ok := d.GetOk("updated_by"); ok {
		req.UpdatedBy = updatedBy.(string)
	}
	if deletedBy, ok := d.GetOk("deleted_by"); ok {
		req.DeletedBy = deletedBy.(string)
	}
	if disabledBy, ok := d.GetOk("disabled_by"); ok {
		req.DisabledBy = disabledBy.(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 createdFrom, ok := d.GetOk("created_from"); ok {
		req.CreatedFrom = createdFrom.(string)
	}
	if createdTo, ok := d.GetOk("created_to"); ok {
		req.CreatedTo = createdTo.(string)
	}

	log.Debugf("utilityAccessGroupUserListCheckPresence")
	userList, err := c.SDN().AccessGroups().UsersList(ctx, req)
	if err != nil {
		return nil, err
	}

	return userList, nil
}
