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 DataSourceDiskListUnattachedCheckPresence(ctx context.Context, state *models.DataSourceDiskListUnattachedModel, c *client.Client) (*disks.ListUnattachedDisks, diag.Diagnostics) {
	diags := diag.Diagnostics{}

	listReq := disks.ListUnattachedRequest{}

	if !state.ByID.IsNull() {
		listReq.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.AccountName.IsNull() {
		listReq.AccountName = state.AccountName.ValueString()
	}
	if !state.DiskMaxSize.IsNull() {
		listReq.DiskMaxSize = state.DiskMaxSize.ValueInt64()
	}
	if !state.Status.IsNull() {
		listReq.Status = state.Status.ValueString()
	}
	if !state.AccountID.IsNull() {
		listReq.AccountID = uint64(state.AccountID.ValueInt64())
	}
	if !state.SepID.IsNull() {
		listReq.SEPID = uint64(state.SepID.ValueInt64())
	}
	if !state.PoolName.IsNull() {
		listReq.Pool = state.PoolName.ValueString()
	}
	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, "DataSourceDiskListUnattachedCheckPresence: before call CloudBroker().Disks().ListUnattached", map[string]any{
		"req": listReq,
	})

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

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