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

func DPDKDataSource(ctx context.Context, state *models.DataSourceDPDKModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.DPDKDataSource")

	diags := diag.Diagnostics{}

	dpdkId := uint64(state.DPDKID.ValueInt64())

	recordDPDK, diags := utilities.DPDKDataSourceCheckPresence(ctx, dpdkId, c)
	if diags.HasError() {
		return diags
	}

	tflog.Info(ctx, "flattens.DPDKDataSource: before flatten", map[string]any{"dpdk_id": dpdkId})

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

		Id:            types.StringValue(id.String()),
		AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.AccountAccess),
		CreatedTime:   types.Int64Value(int64(recordDPDK.CreatedTime)),
		Desc:          types.StringValue(recordDPDK.Description),
		GID:           types.Int64Value(int64(recordDPDK.GID)),
		GUID:          types.Int64Value(int64(recordDPDK.GUID)),
		Name:          types.StringValue(recordDPDK.Name),
		RGAccess:      flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.RGAccess),
		Status:        types.StringValue(recordDPDK.Status),
		OVSBridge:     types.StringValue(recordDPDK.OVSBridge),
		VlanID:        types.Int64Value(int64(recordDPDK.VlanID)),
		ComputeIDs:    flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordDPDK.ComputeIDs),
		UpdatedTime:   types.Int64Value(int64(recordDPDK.UpdatedTime)),
	}

	tflog.Info(ctx, "End flattens.DPDKDataSource", map[string]any{"dpdk_id": dpdkId})
	return nil
}
