package utilities

import (
	"context"

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

func DataSourceDiskListCheckPresence(ctx context.Context, state *models.DisksModel, c *client.Client) (*disks.ListDisks, diag.Diagnostics) {
	diags := diag.Diagnostics{}

	listReq := disks.ListRequest{}

	if !state.ByID.IsNull() {
		listReq.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.Name.IsNull() {
		listReq.Name = state.Name.ValueString()
	}
	if !state.AccountName.IsNull() {
		listReq.AccountName = state.AccountName.ValueString()
	}
	if !state.DiskMaxSize.IsNull() {
		listReq.DiskMaxSize = state.DiskMaxSize.ValueInt64()
	}
	if !state.Shared.IsNull() {
		listReq.Shared = state.Shared.ValueBool()
	}
	if !state.AccountID.IsNull() {
		listReq.AccountID = uint64(state.AccountID.ValueInt64())
	}
	if !state.Type.IsNull() {
		listReq.Type = state.Type.ValueString()
	}
	if !state.SortBy.IsNull() {
		listReq.SortBy = state.SortBy.ValueString()
	}
	if !state.Page.IsNull() {
		listReq.Page = uint64(state.Page.ValueInt64())
	}
	if !state.Size.IsNull() {
		listReq.Size = uint64(state.Size.ValueInt64())
	}

	tflog.Info(ctx, "DataSourceDiskListCheckPresence: before call CloudBroker().Disks().List", map[string]any{
		"req": listReq,
	})

	diskList, err := c.CloudBroker().Disks().List(ctx, listReq)
	if err != nil {
		diags.AddError("Cannot get info about disk list", err.Error())
		return nil, diags
	}

	tflog.Info(ctx, "DataSourceDiskListCheckPresence: got list successfully", map[string]any{
		"entry_count": diskList.EntryCount,
	})
	return diskList, nil
}
