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/decort-golang-sdk/pkg/cloudapi/k8s"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
)

func DataSourceK8sWgCloudInit(ctx context.Context, state *models.RecordK8SWgCloudInitDataSourceModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start FlattenDataSourceK8sWgCloudInit", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WgId.ValueInt64()})

	diags := diag.Diagnostics{}

	metaData, err := c.CloudAPI().K8S().GetWorkerNodesMetaData(ctx, k8s.GetWorkerNodesMetaDataRequest{K8SID: uint64(state.K8SID.ValueInt64()), WorkersGroupID: uint64(state.WgId.ValueInt64())})
	if err != nil {
		diags.AddError("Cannot get info about wg cloud init with error: ", err.Error())
		return diags
	}

	id := uuid.New()

	*state = models.RecordK8SWgCloudInitDataSourceModel{
		K8SID:     state.K8SID,
		WgId:      state.WgId,
		Timeouts:  state.Timeouts,
		Id:        types.StringValue(id.String()),
		CloudInit: types.StringValue(metaData),
	}

	tflog.Info(ctx, "End FlattenDataSourceK8sWgCloudInit", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WgId.ValueInt64()})
	return nil
}
