package utilities

import (
	"context"
	"fmt"

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

func StackListCheckPresence(ctx context.Context, plan *models.ListStacksModel, c *client.Client) (*stack.ListStacks,
	error) {
	req := stack.ListRequest{}

	if !plan.ByID.IsNull() {
		req.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		req.Name = plan.Name.ValueString()
	}
	if !plan.Status.IsNull() {
		req.Status = plan.Status.ValueString()
	}
	if !plan.Type.IsNull() {
		req.Type = plan.Type.ValueString()
	}
	if !plan.SortBy.IsNull() {
		req.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		req.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		req.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "StackListCheckPresence: before call CloudAPI().Stack().List", map[string]any{"req": req})
	stack, err := c.CloudAPI().Stack().List(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("StackListCheckPresence: cannot get info about stack list")
	}

	tflog.Info(ctx, "StackListCheckPresence: response from CloudAPI().Stack().Get", map[string]any{"response": stack})

	return stack, err
}
