package utilities

import (
	"context"
	"fmt"

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

func StackListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceStackListModel, c *client.Client) (*stack.ListStacks, error) {
	tflog.Info(ctx, "StackListDataSourceCheckPresence: Get info about list stacks")
	req := stack.ListRequest{}

	if !plan.ByID.IsNull() {
		req.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		req.Name = plan.Name.ValueString()
	}
	if !plan.Type.IsNull() {
		req.Type = plan.Type.ValueString()
	}
	if !plan.Status.IsNull() {
		req.Status = plan.Status.ValueString()
	}
	if !plan.Page.IsNull() {
		req.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		req.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		req.SortBy = plan.SortBy.ValueString()
	}
	tflog.Info(ctx, "StackListDataSourceCheckPresence: before call CloudBroker().Stack().List", map[string]any{"req": req})

	listStacks, err := c.CloudBroker().Stack().List(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about stack with error: %w", err)
	}
	tflog.Info(ctx, "StackListDataSourceCheckPresence: response from CloudBroker().Stack().List")

	return listStacks, err
}
