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/cloudbroker/account/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/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.ListComputesModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.AccountComputesListDataSource")

	diags := diag.Diagnostics{}

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

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

	*state = models.ListComputesModel{
		AccountID:  state.AccountID,
		Name:       state.Name,
		RGName:     state.RGName,
		RGID:       state.RGID,
		TechStatus: state.TechStatus,
		IpAddress:  state.IpAddress,
		ExtNetName: state.ExtNetName,
		ExtNetID:   state.ExtNetID,
		SortBy:     state.SortBy,
		Page:       state.Page,
		Size:       state.Size,
		Timeouts:   state.Timeouts,

		EntryCount: types.Int64Value(int64(cList.EntryCount)),
	}

	data := make([]models.ItemComputeModel, 0, len(cList.Data))

	for _, v := range cList.Data {
		item := models.ItemComputeModel{
			AccountID:      types.Int64Value(int64(v.AccountID)),
			AccountName:    types.StringValue(v.AccountName),
			CPUs:           types.Int64Value(int64(v.CPUs)),
			CreatedBy:      types.StringValue(v.CreatedBy),
			CreatedTime:    types.Int64Value(int64(v.CreatedTime)),
			DeletedBy:      types.StringValue(v.DeletedBy),
			DeletedTime:    types.Int64Value(int64(v.DeletedTime)),
			ID:             types.Int64Value(int64(v.ID)),
			Name:           types.StringValue(v.Name),
			RAM:            types.Int64Value(int64(v.RAM)),
			Registered:     types.BoolValue(v.Registered),
			RGID:           types.Int64Value(int64(v.RGID)),
			RgName:         types.StringValue(v.RgName),
			Status:         types.StringValue(v.Status),
			TechStatus:     types.StringValue(v.TechStatus),
			TotalDisksSize: types.Int64Value(int64(v.TotalDisksSize)),
			UpdatedBy:      types.StringValue(v.UpdatedBy),
			UpdatedTime:    types.Int64Value(int64(v.UpdatedTime)),
			UserManaged:    types.BoolValue(v.UserManaged),
			VINSConnected:  types.Int64Value(int64(v.VINSConnected)),
		}
		data = append(data, item)
	}

	state.Items = data

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

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