<?php

declare(strict_types=1);

namespace App\Framework\Database\Events;

use App\Framework\Core\ValueObjects\Timestamp;

/**
 * Event that is fired when an entity is loaded from database
 */
final readonly class EntityLoadedEvent
{
    public Timestamp $timestamp;

    public function __construct(
        public object $entity,
        public string $entityClass,
        public mixed $entityId,
        public array $loadedData = [],
        public bool $wasLazy = false,
        ?Timestamp $timestamp = null,
    ) {
        $this->timestamp = $timestamp ?? Timestamp::now();
    }

    /**
     * Check if this event is for a specific entity class
     */
    public function isEntityOfType(string $expectedClass): bool
    {
        return $this->entityClass === $expectedClass || $this->entity instanceof $expectedClass;
    }

    /**
     * Get event metadata
     */
    public function getEventData(): array
    {
        return [
            'event_type' => 'entity_loaded',
            'entity_class' => $this->entityClass,
            'entity_id' => $this->entityId,
            'timestamp' => $this->timestamp->toFloat(),
            'was_lazy' => $this->wasLazy,
            'loaded_data' => $this->loadedData,
        ];
    }
}
