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

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

	diags := diag.Diagnostics{}

	record, err := utilities.VFPoolListCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Cannot get info about fvpool list", err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.VFPoolListDataSource: before flatten", map[string]any{"record": record})

	*state = models.ListVFPoolModel{
		//optional fields
		Timeouts:      state.Timeouts,
		ByID:          state.ByID,
		Name:          state.Name,
		Description:   state.Description,
		Status:        state.Status,
		AccountAccess: state.AccountAccess,
		RgAccess:      state.RgAccess,
		SortBy:        state.SortBy,
		Page:          state.Page,
		Size:          state.Size,

		//compute fields
		EntryCount: types.Int64Value(int64(record.EntryCount)),
	}

	data := make([]models.ItemVFPoolListModel, 0, len(record.Data))
	for _, item := range record.Data {
		i := models.ItemVFPoolListModel{
			VFPoolID:      types.Int64Value(int64(item.ID)),
			AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.AccountAccess),
			CreatedTime:   types.Int64Value(int64(item.CreatedTime)),
			Description:   types.StringValue(item.Description),
			GID:           types.Int64Value(int64(item.GID)),
			GUID:          types.Int64Value(int64(item.GUID)),
			Name:          types.StringValue(item.Name),
			RGAccess:      flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.RGAccess),
			Status:        types.StringValue(item.Status),
			UpdatedTime:   types.Int64Value(int64(item.UpdatedTime)),
		}
		items := make([]models.VFSListModel, 0, len(item.VFS))
		for _, item := range item.VFS {
			i := models.VFSListModel{
				NodeID: types.Int64Value(int64(item.NodeID)),
			}

			vfList := make([]models.VFItemListModel, 0, len(item.VFList))
			for _, vfItem := range item.VFList {
				vfI := models.VFItemListModel{
					NicName: types.StringValue(vfItem.NicName),
				}

				vfInfoList := make([]models.VFSInfoItemListModel, 0, len(vfItem.VFSInfo))
				for _, vfInfoItem := range vfItem.VFSInfo {
					vfInfoI := models.VFSInfoItemListModel{
						ID:      types.Int64Value(int64(vfInfoItem.ID)),
						Claimed: types.BoolValue(vfInfoItem.Claimed),
						VMID:    types.Int64Value(int64(vfInfoItem.VMID)),
					}

					vfInfoList = append(vfInfoList, vfInfoI)

				}

				vfI.VFSInfo = vfInfoList
				vfList = append(vfList, vfI)

			}

			i.VFList = vfList
			items = append(items, i)
		}

		i.VFS = items
		data = append(data, i)
	}
	state.Items = data

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

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