package flattens

import (
	"context"
	"fmt"

	"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"
)

// 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{
			CreatedTime: types.Int64Value(int64(item.CreatedTime)),
			DeletedTime: types.Int64Value(int64(item.DeletedTime)),
			Desc:        types.StringValue(item.Description),
			AccountID:   types.Int64Value(int64(item.ID)),
			AccountName: types.StringValue(item.Name),
			Status:      types.StringValue(item.Status),
			UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
		}

		i.ComputeFeatures, diags = types.ListValueFrom(ctx, types.StringType, item.ComputeFeatures)
		if diags.HasError() {
			tflog.Error(ctx, fmt.Sprint("flattens.AccountListDeletedDataSource: cannot flatten item.ComputeFeatures to i.ComputeFeatures", diags))
		}

		aclList := make([]models.RecordACLModel, 0, len(item.ACL))
		for _, acl := range item.ACL {
			a := models.RecordACLModel{
				Explicit:    types.BoolValue(acl.IsExplicit),
				GUID:        types.StringValue(acl.GUID),
				Right:       types.StringValue(acl.Rights),
				Status:      types.StringValue(acl.Status),
				Type:        types.StringValue(acl.Type),
				UserGroupID: types.StringValue(acl.UgroupID),
			}
			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
}
