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

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

	diags := diag.Diagnostics{}

	computesList, err := utilities.AccountComputesListCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Cannot get info about account computes list", err.Error())
		return diags
	}

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

	id := uuid.New()
	*state = models.DataSourceAccountComputesListModel{
		AccountID:  state.AccountID,
		ComputeID:  state.ComputeID,
		Name:       state.Name,
		RGName:     state.RGName,
		RGID:       state.RGID,
		TechStatus: state.TechStatus,
		IPAddress:  state.IPAddress,
		ExtNetName: state.ExtNetName,
		ExtNetID:   state.ExtNetID,
		Page:       state.Page,
		Size:       state.Size,
		Timeouts:   state.Timeouts,
		SortBy:     state.SortBy,

		Id:         types.StringValue(id.String()),
		EntryCount: types.Int64Value(int64(computesList.EntryCount)),
	}

	items := make([]models.ItemComputeModel, 0, len(computesList.Data))
	for _, item := range computesList.Data {
		i := models.ItemComputeModel{
			AccountID:      types.Int64Value(int64(item.AccountID)),
			AccountName:    types.StringValue(item.AccountName),
			CPUs:           types.Int64Value(int64(item.CPUs)),
			CreatedBy:      types.StringValue(item.CreatedBy),
			CreatedTime:    types.Int64Value(int64(item.CreatedTime)),
			DeletedBy:      types.StringValue(item.DeletedBy),
			DeletedTime:    types.Int64Value(int64(item.DeletedTime)),
			ComputeID:      types.Int64Value(int64(item.ComputeID)),
			ComputeName:    types.StringValue(item.ComputeName),
			RAM:            types.Int64Value(int64(item.RAM)),
			Registered:     types.BoolValue(item.Registered),
			RGID:           types.Int64Value(int64(item.RGID)),
			RGName:         types.StringValue(item.RGName),
			Status:         types.StringValue(item.Status),
			TechStatus:     types.StringValue(item.TechStatus),
			TotalDisksSize: types.Int64Value(int64(item.TotalDisksSize)),
			UpdatedBy:      types.StringValue(item.UpdatedBy),
			UpdatedTime:    types.Int64Value(int64(item.UpdatedTime)),
			UserManaged:    types.BoolValue(item.UserManaged),
			VINSConnected:  types.Int64Value(int64(item.VINSConnected)),
		}
		items = append(items, i)
	}
	state.Items = items

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

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