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

	rgListReq := account.ListRGRequest{AccountID: uint64(plan.AccountID.ValueInt64())}

	if !plan.RGID.IsNull() {
		rgListReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.VinsID.IsNull() {
		rgListReq.VINSID = uint64(plan.VinsID.ValueInt64())
	}
	if !plan.VMID.IsNull() {
		rgListReq.VMID = uint64(plan.VMID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		rgListReq.Name = plan.Name.ValueString()
	}
	if !plan.Status.IsNull() {
		rgListReq.Status = plan.Status.ValueString()
	}
	if !plan.Page.IsNull() {
		rgListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		rgListReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		rgListReq.SortBy = plan.SortBy.ValueString()
	}

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

	tflog.Info(ctx, "AccountRGListCheckPresence: response from CloudAPI().Account().ListRG")

	return rgList, err
}
