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/extnet/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/extnet/utilities"
)

// ExtNetDefaultDataSource flattens data source for extnet default.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func ExtNetDefaultDataSource(ctx context.Context, state *models.DataSourceExtNetDefaultModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.ExtNetDefaultDataSource")

	diags := diag.Diagnostics{}

	netId, err := utilities.ExtNetDefaultCheckPresence(ctx, c)
	if err != nil {
		diags.AddError("Cannot get info about default extnet ID %v", err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.ExtNetDefaultDataSource: before flatten")

	id := uuid.New()
	*state = models.DataSourceExtNetDefaultModel{
		Timeouts: state.Timeouts,

		Id:    types.StringValue(id.String()),
		NetId: types.Int64Value(int64(netId)),
	}

	tflog.Info(ctx, "flattens.ExtNetDefaultDataSource: after flatten")

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