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 AccountListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceAccountListModel, c *client.Client) (*account.ListAccounts, error) {
	tflog.Info(ctx, "AccountListDataSourceCheckPresence: Get info about list accounts")
	req := account.ListRequest{}

	if !plan.ByID.IsNull() {
		req.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		req.Name = plan.Name.ValueString()
	}
	if !plan.ACL.IsNull() {
		req.ACL = plan.ACL.ValueString()
	}
	if !plan.Status.IsNull() {
		req.Status = plan.Status.ValueString()
	}
	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, "AccountListDataSourceCheckPresence: before call CloudBroker().Account().List", map[string]any{"req": req})

	listAccounts, err := c.CloudBroker().Account().List(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about accounts with error: %w", err)
	}
	tflog.Info(ctx, "AccountListDataSourceCheckPresence: response from CloudBroker().Account().List")

	return listAccounts, err
}
