<?php

declare(strict_types=1);

namespace App\Framework\Database\Events\Examples;

use App\Framework\Database\EntityManager;

/**
 * Comprehensive Entity Event System usage examples
 *
 * This demonstrates how to use the entity lifecycle events
 * with the existing Core EventDispatcher system.
 */
final class EntityEventUsageExample
{
    public function __construct(
        private readonly EntityManager $entityManager
    ) {
    }

    /**
     * Example: Basic lifecycle event handling
     */
    public function basicLifecycleEventHandling(): void
    {
        // Create a new entity
        $user = new ExampleUser('john@example.com', 'John Doe');

        // The EntityManager will automatically dispatch EntityCreatedEvent
        $this->entityManager->insert($user);

        // Update the entity
        $user = $user->updateEmail('john.doe@example.com');

        // The EntityManager will automatically dispatch EntityUpdatedEvent
        $this->entityManager->update($user);

        // Delete the entity
        // The EntityManager will automatically dispatch EntityDeletedEvent
        $this->entityManager->delete($user);
    }

    /**
     * Example: Domain event recording and dispatching
     */
    public function domainEventHandling(): void
    {
        $user = new ExampleUser('jane@example.com', 'Jane Smith');

        // Record domain events (these are not automatically dispatched)
        $welcomeEvent = new UserWelcomeEmailEvent($user, 'jane@example.com');
        $this->entityManager->recordDomainEvent($user, $welcomeEvent);

        $analyticsEvent = new UserRegistrationAnalyticsEvent($user, 'web', 'organic');
        $this->entityManager->recordDomainEvent($user, $analyticsEvent);

        // Create the entity
        $this->entityManager->insert($user);

        // Manually dispatch domain events for this entity
        $this->entityManager->dispatchDomainEventsForEntity($user);

        // Or dispatch all domain events across all entities
        // $this->entityManager->dispatchAllDomainEvents();
    }

    /**
     * Example: Event statistics and monitoring
     */
    public function eventStatistics(): array
    {
        // Get domain event statistics
        $domainStats = $this->entityManager->getDomainEventStats();

        return [
            'tracked_entities' => $domainStats['tracked_entities'],
            'pending_domain_events' => $domainStats['total_events'],
        ];
    }

    /**
     * Example: Batch operations with events
     */
    public function batchOperationsWithEvents(): void
    {
        $users = [
            new ExampleUser('user1@example.com', 'User One'),
            new ExampleUser('user2@example.com', 'User Two'),
            new ExampleUser('user3@example.com', 'User Three'),
        ];

        // Record domain events for each user
        foreach ($users as $user) {
            $event = new UserRegistrationAnalyticsEvent($user, 'batch_import', 'admin');
            $this->entityManager->recordDomainEvent($user, $event);
        }

        // Save all users (will trigger EntityCreatedEvent for each)
        $this->entityManager->saveAll(...$users);

        // Dispatch all domain events at once
        $this->entityManager->dispatchAllDomainEvents();
    }

    /**
     * Example: Conditional event handling
     */
    public function conditionalEventHandling(): void
    {
        $user = new ExampleUser('conditional@example.com', 'Conditional User');

        // Record conditional domain event
        $premiumEvent = new UserPremiumUpgradeEvent($user, 'premium_plan');
        $this->entityManager->recordDomainEvent($user, $premiumEvent);

        // Events will be dispatched based on conditions in handlers
        $this->entityManager->insert($user);
        $this->entityManager->dispatchDomainEventsForEntity($user);
    }
}
