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

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

	diags := diag.Diagnostics{}

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

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

	id := uuid.New()
	*state = models.DataSourceAccountListDeletedModel{
		ByID:     state.ByID,
		Name:     state.Name,
		ACL:      state.ACL,
		Page:     state.Page,
		Size:     state.Size,
		Timeouts: state.Timeouts,
		SortBy:   state.SortBy,

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

	items := make([]models.ItemAccountListDeletedModel, 0, len(accListDel.Data))
	for _, item := range accListDel.Data {
		i := models.ItemAccountListDeletedModel{
			DCLocation:             types.StringValue(item.DCLocation),
			Ckey:                   types.StringValue(item.CKey),
			Meta:                   flattens.FlattenSimpleTypeToList(ctx, types.StringType, item.Meta),
			ComputeFeatures:        flattens.FlattenSimpleTypeToList(ctx, types.StringType, item.ComputeFeatures),
			Company:                types.StringValue(item.Company),
			Companyurl:             types.StringValue(item.CompanyURL),
			CpuAllocationParameter: types.StringValue(item.CPUAllocationParameter),
			CpuAllocationRatio:     types.Float64Value(float64(item.CPUAllocationRatio)),
			CreatedTime:            types.Int64Value(int64(item.CreatedTime)),
			CreatedBy:              types.StringValue(item.CreatedBy),
			DeletedTime:            types.Int64Value(int64(item.DeletedTime)),
			DeletedBy:              types.StringValue(item.DeletedBy),
			AccountID:              types.Int64Value(int64(item.ID)),
			GUID:                   types.Int64Value(int64(item.GUID)),
			AccountName:            types.StringValue(item.Name),
			Status:                 types.StringValue(item.Status),
			UpdatedTime:            types.Int64Value(int64(item.UpdatedTime)),
			ResourceLimits: &models.AccountLimitsModel{
				CUC:      types.Float64Value(float64(item.ResourceLimits.CuC)),
				CUD:      types.Float64Value(float64(item.ResourceLimits.CuD)),
				CUI:      types.Float64Value(float64(item.ResourceLimits.CuI)),
				CUM:      types.Float64Value(float64(item.ResourceLimits.CuM)),
				CUDM:     types.Float64Value(float64(item.ResourceLimits.CuDM)),
				CUNP:     types.Float64Value(float64(item.ResourceLimits.CuNP)),
				GPUUnits: types.Float64Value(float64(item.ResourceLimits.GPUUnits)),
			},
			ResourceTypes:    flattens.FlattenSimpleTypeToList(ctx, types.StringType, item.ResTypes),
			SendAccessEmails: types.BoolValue(item.SendAccessEmails),
			UniqPools:        flattens.FlattenSimpleTypeToList(ctx, types.StringType, item.UniqPools),
			Version:          types.Int64Value(int64(item.Version)),
			Vins:             flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.VINS),
		}

		aclList := make([]models.RecordACLModel, 0, len(item.ACL))
		for _, acl := range item.ACL {
			a := models.RecordACLModel{
				Explicit:    types.BoolValue(acl.Explicit),
				GUID:        types.StringValue(acl.GUID),
				Right:       types.StringValue(acl.Right),
				Status:      types.StringValue(acl.Status),
				Type:        types.StringValue(acl.Type),
				UserGroupID: types.StringValue(acl.UserGroupID),
			}
			aclList = append(aclList, a)
		}

		i.ACL = aclList
		items = append(items, i)
	}
	state.Items = items

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

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