package utilities

import (
	"context"
	"fmt"

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

func AccountComputesListDataSourceCheckPresence(ctx context.Context, plan *models.ListComputesModel, c *client.Client) (*account.ListComputes, error) {
	tflog.Info(ctx, "AccountComputesListDataSourceCheckPresence: Get info about list accounts")
	req := account.ListComputesRequest{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.ComputeID.IsNull() {
		req.ComputeID = uint64(plan.ComputeID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		req.Name = plan.Name.ValueString()
	}
	if !plan.RGName.IsNull() {
		req.RGName = plan.RGName.ValueString()
	}
	if !plan.RGID.IsNull() {
		req.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.TechStatus.IsNull() {
		req.TechStatus = plan.TechStatus.ValueString()
	}
	if !plan.IpAddress.IsNull() {
		req.IPAddress = plan.IpAddress.ValueString()
	}
	if !plan.ExtNetName.IsNull() {
		req.ExtNetName = plan.ExtNetName.ValueString()
	}
	if !plan.ExtNetID.IsNull() {
		req.ExtNetID = uint64(plan.ExtNetID.ValueInt64())
	}
	if !plan.Page.IsNull() {
		req.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		req.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		req.SortBy = plan.SortBy.ValueString()
	}
	tflog.Info(ctx, "AccountComputesListDataSourceCheckPresence: before call CloudBroker().Account().List", map[string]any{"req": req})

	listComputes, err := c.CloudBroker().Account().ListComputes(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cannot get list computes with error: %w", err)
	}
	tflog.Info(ctx, "AccountComputesListDataSourceCheckPresence: response from CloudBroker().Account().List")

	return listComputes, err
}
