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

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

	diags := diag.Diagnostics{}

	resConsList, err := utilities.RGResourceConsumptionListCheckPresence(ctx, c)
	if err != nil {
		diags.AddError("Cannot get info about resource group resource consumption list", err.Error())
		return diags
	}

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

	id := uuid.New()
	*state = models.DataSourceRGResourceConsumptionListModel{
		Timeouts: state.Timeouts,
		Id:       types.StringValue(id.String()),
	}

	items := make([]models.ItemResourceConsumptionModel, 0, len(resConsList.Data))
	for _, resConsItem := range resConsList.Data {
		item := models.ItemResourceConsumptionModel{
			RGID: types.Int64Value(int64(resConsItem.RGID)),
			Consumed: &models.ResourceModel{
				CPU:         types.Int64Value(resConsItem.Consumed.CPU),
				DiskSize:    types.Float64Value(resConsItem.Consumed.DiskSize),
				DiskSizeMax: types.Float64Value(resConsItem.Consumed.DiskSizeMax),
				ExtIPs:      types.Int64Value(resConsItem.Consumed.ExtIPs),
				ExtTraffic:  types.Int64Value(resConsItem.Consumed.ExtTraffic),
				GPU:         types.Int64Value(resConsItem.Consumed.GPU),
				RAM:         types.Int64Value(resConsItem.Consumed.RAM),
			},
			Reserved: &models.ResourceModel{
				CPU:         types.Int64Value(resConsItem.Reserved.CPU),
				DiskSize:    types.Float64Value(resConsItem.Reserved.DiskSize),
				DiskSizeMax: types.Float64Value(resConsItem.Reserved.DiskSizeMax),
				ExtIPs:      types.Int64Value(resConsItem.Reserved.ExtIPs),
				ExtTraffic:  types.Int64Value(resConsItem.Reserved.ExtTraffic),
				GPU:         types.Int64Value(resConsItem.Reserved.GPU),
				RAM:         types.Int64Value(resConsItem.Reserved.RAM),
			},
			ResourceLimits: &models.ResourceLimitsModel{
				CUC:      types.Float64Value(resConsItem.ResourceLimits.CUC),
				CUD:      types.Float64Value(resConsItem.ResourceLimits.CUD),
				CUDM:     types.Float64Value(resConsItem.ResourceLimits.CUDM),
				CUI:      types.Float64Value(resConsItem.ResourceLimits.CUI),
				CUM:      types.Float64Value(resConsItem.ResourceLimits.CUM),
				CUNP:     types.Float64Value(resConsItem.ResourceLimits.CUNP),
				GPUUnits: types.Float64Value(resConsItem.ResourceLimits.GPUUnits),
			},
		}

		sepsConsumed := make([]models.SEPsModel, 0, len(resConsItem.Consumed.SEPs))
		for sepId, data := range resConsItem.Consumed.SEPs {
			for dataName, diskData := range data {
				sepItem := models.SEPsModel{
					SepID:       types.StringValue(sepId),
					DataName:    types.StringValue(dataName),
					DiskSize:    types.Float64Value(diskData.DiskSize),
					DiskSizeMax: types.Float64Value(diskData.DiskSizeMax),
				}
				sepsConsumed = append(sepsConsumed, sepItem)
			}
		}
		item.Consumed.SEPs = sepsConsumed

		sepsReserved := make([]models.SEPsModel, 0, len(resConsItem.Reserved.SEPs))
		for sepId, data := range resConsItem.Reserved.SEPs {
			for dataName, diskData := range data {
				sepItem := models.SEPsModel{
					SepID:       types.StringValue(sepId),
					DataName:    types.StringValue(dataName),
					DiskSize:    types.Float64Value(diskData.DiskSize),
					DiskSizeMax: types.Float64Value(diskData.DiskSizeMax),
				}
				sepsReserved = append(sepsReserved, sepItem)
			}
		}
		item.Reserved.SEPs = sepsReserved

		items = append(items, item)
	}

	state.Items = items
	state.EntryCount = types.Int64Value(int64(resConsList.EntryCount))

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

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