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/node"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/node/models"
)

func NodeListDataSourceCheckPresence(ctx context.Context, state *models.DataSourceNodeList, c *client.Client) (*node.ListNodes, diag.Diagnostics) {
	tflog.Info(ctx, "NodeListDataSourceCheckPresence: Get node list info")

	diags := diag.Diagnostics{}

	req := node.ListRequest{}

	if !state.ByID.IsNull() {
		req.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.Name.IsNull() {
		req.Name = state.Name.ValueString()
	}
	if !state.Version.IsNull() {
		req.Version = state.Version.ValueString()
	}
	if !state.Release.IsNull() {
		req.Release = state.Release.ValueString()
	}
	if !state.SepID.IsNull() {
		req.SepID = uint64(state.SepID.ValueInt64())
	}
	if !state.Role.IsNull() {
		req.Role = state.Role.ValueString()
	}
	if !state.Status.IsNull() {
		req.Status = state.Status.ValueString()
	}
	if !state.SortBy.IsNull() {
		req.SortBy = state.SortBy.ValueString()
	}
	if !state.Page.IsNull() {
		req.Page = uint64(state.Page.ValueInt64())
	}
	if !state.Size.IsNull() {
		req.Size = uint64(state.Size.ValueInt64())
	}

	recordNodeList, err := c.CloudBroker().Node().List(ctx, req)
	if err != nil {
		diags.AddError("Cannot get info about Node list", err.Error())
		return nil, diags
	}

	tflog.Info(ctx, "NodeListDataSourceCheckPresence: response from CloudBroker().Node().List")

	return recordNodeList, nil
}
