package utilities

import (
	"context"
	"fmt"

	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/rg/models"

	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/rg"
)

func RGAffinityGroupsListCheckPresence(ctx context.Context, plan *models.DataSourceRGAffinityGroupsListModel, c *client.Client) (*rg.ListAffinityGroups, error) {
	agListReq := rg.AffinityGroupsListRequest{RGID: uint64(plan.RGID.ValueInt64())}

	if !plan.Page.IsNull() {
		agListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		agListReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "RGAffinityGroupsListCheckPresence: before call CloudAPI().RG().AffinityGroupsList", map[string]any{"req": agListReq})
	agList, err := c.CloudAPI().RG().AffinityGroupsList(ctx, agListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about resource group affinity groups list with error: %w", err)
	}

	tflog.Info(ctx, "RGAffinityGroupsListCheckPresence: response from CloudAPI().RG().AffinityGroupsList", map[string]any{"response": agList})

	return agList, err
}
