package collection

import (
	"context"

	"github.com/gocql/gocql"
	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/collection"
)

// RemoveUserFromAllCollectionsUseCase removes a user from all collections they are a member of
// Used for GDPR right-to-be-forgotten implementation
type RemoveUserFromAllCollectionsUseCase interface {
	Execute(ctx context.Context, userID gocql.UUID, userEmail string) (int, error)
}

type removeUserFromAllCollectionsUseCaseImpl struct {
	logger *zap.Logger
	repo   collection.CollectionRepository
}

// NewRemoveUserFromAllCollectionsUseCase creates a new use case for removing user from all collections
func NewRemoveUserFromAllCollectionsUseCase(
	logger *zap.Logger,
	repo collection.CollectionRepository,
) RemoveUserFromAllCollectionsUseCase {
	return &removeUserFromAllCollectionsUseCaseImpl{
		logger: logger.Named("RemoveUserFromAllCollectionsUseCase"),
		repo:   repo,
	}
}

func (uc *removeUserFromAllCollectionsUseCaseImpl) Execute(ctx context.Context, userID gocql.UUID, userEmail string) (int, error) {
	uc.logger.Info("Removing user from all shared collections",
		zap.String("user_id", userID.String()))

	modifiedCollections, err := uc.repo.RemoveUserFromAllCollections(ctx, userID, userEmail)
	if err != nil {
		uc.logger.Error("Failed to remove user from all collections",
			zap.String("user_id", userID.String()),
			zap.Error(err))
		return 0, err
	}

	count := len(modifiedCollections)

	uc.logger.Info("✅ Successfully removed user from all shared collections",
		zap.String("user_id", userID.String()),
		zap.Int("collections_modified", count))

	return count, nil
}
