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 ExtNetReservedIPCheckPresence(ctx context.Context, plan *models.DataSourceExtNetReservedIPModel, c *client.Client) ([]extnet.RecordReservedIP, error) {
	tflog.Info(ctx, "ExtNetReservedIPCheckPresence: Get info about extnet reserved ip")

	extnetReservedIPReq := extnet.GetReservedIP{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.ExtNetID.IsNull() {
		extnetReservedIPReq.ExtNetID = uint64(plan.ExtNetID.ValueInt64())
	}

	tflog.Info(ctx, "ExtNetReservedIPCheckPresence: before call CloudAPI().ExtNet().GetReservedIP", map[string]any{"req": extnetReservedIPReq})
	extNetReservedIP, err := c.CloudAPI().ExtNet().GetReservedIP(ctx, extnetReservedIPReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about extnet with error: %w", err)
	}

	tflog.Info(ctx, "ExtNetReservedIPCheckPresence: response from CloudAPI().ExtNet().GetReservedIP", map[string]any{"response": extnetReservedIPReq})

	return extNetReservedIP, err
}
