package flattens

import (
	"context"

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

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

	diags := diag.Diagnostics{}

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

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

	*state = models.AccountGetResourceConsumptionListModel{
		EntryCount: state.EntryCount,
		Timeouts:   state.Timeouts,
	}

	items := make([]models.AccountGetResourceConsumptionListItemModel, 0, len(resConsList.Data))
	for _, resConsItem := range resConsList.Data {
		item := models.AccountGetResourceConsumptionListItemModel{
			AccountId: types.Int64Value(int64(resConsItem.AccountID)),
			Consumed: &models.ResourceConsumptionListModel{
				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.ResourceConsumptionListModel{
				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),
			},
		}

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

		sepsReserved := make([]models.ResourceConsumptionSepListModel, 0, len(resConsItem.Reserved.SEPs))
		for sepId, data := range resConsItem.Reserved.SEPs {
			for dataName, diskData := range data {
				sepItem := models.ResourceConsumptionSepListModel{
					SepID:       types.StringValue(sepId),
					PoolName:    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.AccountGetResourceConsumptionList: after flatten")

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