package flattens

import (
	"context"

	"github.com/google/uuid"
	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/kvmvm/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/kvmvm/utilities"
)

func DataSourceComputeGetConsoleUrl(ctx context.Context, state *models.GetConsoleUrlModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start FlattenDataSourceComputeGetConsoleUrl")

	diags := diag.Diagnostics{}

	consoleUrl, err := utilities.ComputeGetConsoleUrlCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Error get compute console url", err.Error())
		return diags
	}

	id := uuid.New()

	*state = models.GetConsoleUrlModel{
		ComputeID:  state.ComputeID,
		Timeouts:   state.Timeouts,
		Id:         types.StringValue(id.String()),
		ConsoleURL: types.StringValue(consoleUrl),
	}

	tflog.Info(ctx, "End FlattenDataSourceComputeGetConsoleUrl")
	return nil
}
