package accessgroup

import (
	"context"

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

func utilityAccessGroupCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*acsgroups.AccessGroup, error) {
	c := m.(*controller.ControllerCfg)

	req := acsgroups.GetGroupRequest{}

	if d.Id() != "" {
		req.GroupID = d.Id()
	} else {
		req.GroupID = d.Get("access_group_id").(string)
	}

	accessGroup, err := c.SDN().AccessGroups().Get(ctx, req)
	if err != nil {
		return nil, err
	}

	return accessGroup, nil
}

func utilityAccessGroupUsersGet(ctx context.Context, groupID string, m interface{}) (*acsgroups.UsersList, error) {
	c := m.(*controller.ControllerCfg)

	req := acsgroups.UsersListRequest{
		AccessGroupID: groupID,
	}

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

	return userList, nil
}
