package flattens

import (
	"context"
	"strconv"

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

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

	diags := diag.Diagnostics{}

	recordK8ci, diags := utilities.K8CIResourceCheckPresence(ctx, state, c)
	if diags.HasError() {
		return diags
	}

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

	*state = models.ResourceK8CIModel{

		Name:           state.Name,
		Version:        state.Version,
		MasterDriver:   state.MasterDriver,
		MasterImageId:  state.MasterImageId,
		MaxMasterCount: state.MaxMasterCount,
		MaxWorkerCount: state.MaxWorkerCount,
		NetworkPlugins: state.NetworkPlugins,
		WorkerDriver:   state.WorkerDriver,
		WorkerImageId:  state.WorkerImageId,
		Timeouts:       state.Timeouts,
		Description:    state.Description,
		Enabled:        state.Enabled,
		Permanently:    state.Permanently,
		Restore:        state.Restore,
		SharedWith:     state.SharedWith,

		GID:        types.Int64Value(int64(recordK8ci.GID)),
		GUID:       types.Int64Value(int64(recordK8ci.GUID)),
		Id:         types.StringValue(strconv.Itoa(int(recordK8ci.ID))),
		K8ciID:     types.Int64Value(int64(recordK8ci.ID)),
		LBImageID:  types.Int64Value(int64(recordK8ci.LBImageID)),
		Milestones: types.Int64Value(int64(recordK8ci.Milestones)),
		Status:     types.StringValue(recordK8ci.Status),
	}

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