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

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

	diags := diag.Diagnostics{}

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

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

	id := uuid.New()
	*state = models.DataSourceRGListModel{
		ById:           state.ById,
		Name:           state.Name,
		AccountId:      state.AccountId,
		AccountName:    state.AccountName,
		CreatedAfter:   state.CreatedAfter,
		CreatedBefore:  state.CreatedBefore,
		Status:         state.Status,
		LockStatus:     state.LockStatus,
		IncludeDeleted: state.IncludeDeleted,
		SortBy:         state.SortBy,
		Page:           state.Page,
		Size:           state.Size,
		Timeouts:       state.Timeouts,
		Id:             types.StringValue(id.String()),
	}

	items := make([]models.ItemsRGListModel, 0, len(rgList.Data))
	for _, rgItem := range rgList.Data {
		item := models.ItemsRGListModel{
			AccountACL:             flattenACL(ctx, &rgItem.ACL),
			AccountID:              types.Int64Value(int64(rgItem.AccountID)),
			AccountName:            types.StringValue(rgItem.AccountName),
			ComputeFeatures:        flattens.FlattenSimpleTypeToList(ctx, types.StringType, &rgItem.ComputeFeatures),
			CPUAllocationParameter: types.StringValue(rgItem.CPUAllocationParameter),
			CPUAllocationRatio:     types.Float64Value(rgItem.CPUAllocationRatio),
			CreatedBy:              types.StringValue(rgItem.CreatedBy),
			CreatedTime:            types.Int64Value(int64(rgItem.CreatedTime)),
			DefNetID:               types.Int64Value(rgItem.DefNetID),
			DefNetType:             types.StringValue(rgItem.DefNetType),
			DeletedBy:              types.StringValue(rgItem.DeletedBy),
			DeletedTime:            types.Int64Value(int64(rgItem.DeletedTime)),
			Description:            types.StringValue(rgItem.Description),
			Dirty:                  types.BoolValue(rgItem.Dirty),
			GID:                    types.Int64Value(int64(rgItem.GID)),
			GUID:                   types.Int64Value(int64(rgItem.GUID)),
			RGID:                   types.Int64Value(int64(rgItem.ID)),
			LockStatus:             types.StringValue(rgItem.LockStatus),
			Milestones:             types.Int64Value(int64(rgItem.Milestones)),
			Name:                   types.StringValue(rgItem.Name),
			ResTypes:               flattens.FlattenSimpleTypeToList(ctx, types.StringType, &rgItem.ResTypes),
			ResourceLimits:         flattenResourceLimits(ctx, &rgItem.ResourceLimits),
			Secret:                 types.StringValue(rgItem.Secret),
			Status:                 types.StringValue(rgItem.Status),
			UniqPools:              flattens.FlattenSimpleTypeToList(ctx, types.StringType, &rgItem.UniqPools),
			UpdatedBy:              types.StringValue(rgItem.UpdatedBy),
			UpdatedTime:            types.Int64Value(int64(rgItem.UpdatedTime)),
			VINS:                   flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, &rgItem.VINS),
			VMS:                    flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, &rgItem.VMs),
		}

		items = append(items, item)
	}

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

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

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