package utilities

import (
	"context"
	"fmt"

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

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

func ExtNetComputesListCheckPresence(ctx context.Context, plan *models.DataSourceExtNetComputesListModel, c *client.Client) (*extnet.ListExtNetComputes, error) {
	tflog.Info(ctx, "ExtNetComputesListCheckPresence: Get info about extnet list")

	listCompReq := extnet.ListComputesRequest{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.RGID.IsNull() {
		listCompReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.ComputeID.IsNull() {
		listCompReq.ComputeID = uint64(plan.ComputeID.ValueInt64())
	}
	if !plan.Page.IsNull() {
		listCompReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listCompReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		listCompReq.SortBy = plan.SortBy.ValueString()
	}

	tflog.Info(ctx, "ExtNetComputesListCheckPresence: before call CloudAPI().ExtNet().ListComputes", map[string]any{"req": listCompReq})
	compList, err := c.CloudAPI().ExtNet().ListComputes(ctx, listCompReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about extnet list computes with error: %w", err)
	}

	tflog.Info(ctx, "ExtNetComputesListCheckPresence: response from CloudAPI().ExtNet().ListComputes", map[string]any{"response": compList})

	return compList, err
}
