package flattens

import (
	"context"
	"fmt"

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

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

	diags := diag.Diagnostics{}

	agCompsList, err := utilities.RGAffinityGroupComputesCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Cannot get info about resource group affinity group computes", err.Error())
		return diags
	}

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

	id := uuid.New()
	*state = models.DataSourceRGAffinityGroupComputesModel{
		RGID:          state.RGID,
		AffinityGroup: state.AffinityGroup,

		Timeouts: state.Timeouts,
		Id:       types.StringValue(id.String()),
	}

	var diagsItem diag.Diagnostics
	items := make([]models.ItemAffinityGroupComputeModel, 0, len(*agCompsList))
	for _, comp := range *agCompsList {
		item := models.ItemAffinityGroupComputeModel{
			ComputeID: types.Int64Value(int64(comp.ComputeID)),
		}

		item.OtherNode, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.OtherNode)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.OtherNode to item.OtherNode", diags))
		}
		item.OtherNodeIndirect, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.OtherNodeIndirect)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.OtherNodeIndirect to item.OtherNodeIndirect", diags))
		}
		item.OtherNodeIndirectSoft, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.OtherNodeIndirectSoft)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.OtherNodeIndirectSoft to item.OtherNodeIndirectSoft", diags))
		}
		item.OtherNodeSoft, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.OtherNodeSoft)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.OtherNodeSoft to item.OtherNodeSoft", diags))
		}
		item.SameNode, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.SameNode)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.SameNode to item.SameNode", diags))
		}
		item.SameNodeSoft, diagsItem = types.ListValueFrom(ctx, types.Int64Type, comp.SameNodeSoft)
		if diagsItem.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.RGAffinityGroupComputesDataSource: cannot flatten comp.SameNodeSoft to item.SameNodeSoft", diags))
		}

		items = append(items, item)
	}

	state.Items = items

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

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