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

	accListReq := account.ListRequest{}

	if !plan.ByID.IsNull() {
		accListReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		accListReq.Name = plan.Name.ValueString()
	}
	if !plan.ACL.IsNull() {
		accListReq.ACL = plan.ACL.ValueString()
	}
	if !plan.Status.IsNull() {
		accListReq.Status = plan.Status.ValueString()
	}
	if !plan.Page.IsNull() {
		accListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		accListReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		accListReq.SortBy = plan.SortBy.ValueString()
	}

	tflog.Info(ctx, "AccountListCheckPresence: before call CloudAPI().Account().List", map[string]any{"req": accListReq})
	accList, err := c.CloudAPI().Account().List(ctx, accListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about account with error: %w", err)
	}

	tflog.Info(ctx, "AccountListCheckPresence: response from CloudAPI().Account().List")

	return accList, err
}
