package utilities

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/account"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/account/models"
)

func AccountComputesListCheckPresence(ctx context.Context, plan *models.DataSourceAccountComputesListModel, c *client.Client) (*account.ListComputes, error) {
	tflog.Info(ctx, "AccountComputesListCheckPresence: Get info about account computes list")

	computesListReq := account.ListComputesRequest{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.ComputeID.IsNull() {
		computesListReq.ComputeID = uint64(plan.ComputeID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		computesListReq.Name = plan.Name.ValueString()
	}
	if !plan.RGName.IsNull() {
		computesListReq.RGName = plan.RGName.ValueString()
	}
	if !plan.RGID.IsNull() {
		computesListReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.TechStatus.IsNull() {
		computesListReq.TechStatus = plan.TechStatus.ValueString()
	}
	if !plan.IPAddress.IsNull() {
		computesListReq.IPAddress = plan.IPAddress.ValueString()
	}
	if !plan.ExtNetName.IsNull() {
		computesListReq.ExtNetName = plan.ExtNetName.ValueString()
	}
	if !plan.ExtNetID.IsNull() {
		computesListReq.ExtNetID = uint64(plan.ExtNetID.ValueInt64())
	}
	if !plan.Page.IsNull() {
		computesListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		computesListReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Size.IsNull() {
		computesListReq.Size = uint64(plan.Size.ValueInt64())
	}

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

	tflog.Info(ctx, "AccountComputesListCheckPresence: response from CloudAPI().Account().ListComputes")

	return computesList, err
}
