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

	disksListReq := account.ListDisksRequest{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.DiskID.IsNull() {
		disksListReq.DiskID = uint64(plan.DiskID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		disksListReq.Name = plan.Name.ValueString()
	}
	if !plan.DiskMaxSize.IsNull() {
		disksListReq.DiskMaxSize = uint64(plan.DiskMaxSize.ValueInt64())
	}
	if !plan.Type.IsNull() {
		disksListReq.Type = plan.Type.ValueString()
	}
	if !plan.Page.IsNull() {
		disksListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		disksListReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		disksListReq.SortBy = plan.SortBy.ValueString()
	}

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

	tflog.Info(ctx, "AccountDisksListCheckPresence: response from CloudAPI().Account().ListDisks")

	return disksList, err
}
