package utilities

import (
	"context"
	"strings"

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

func ComputeGetConsoleUrlCheckPresence(ctx context.Context, state *models.GetConsoleUrlModel, c *client.Client) (string, error) {
	tflog.Info(ctx, "Get compute console url")

	req := compute.GetConsoleURLRequest{
		ComputeID: uint64(state.ComputeID.ValueInt64()),
	}

	tflog.Info(ctx, "Check req", map[string]any{"req": req})

	consoleUrl, err := c.CloudAPI().Compute().GetConsoleURL(ctx, req)
	if err != nil {
		return "", err
	}

	result := strings.ReplaceAll(consoleUrl, "\"", "")
	result = strings.ReplaceAll(result, "\\", "")

	tflog.Info(ctx, "Getting compute console url, successfully")
	return result, nil
}
