<?php

declare(strict_types=1);

namespace App\Framework\EventSourcing\Snapshots;

use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\EventSourcing\AggregateId;

/**
 * Snapshot Value Object
 *
 * Immutable snapshot of aggregate state at specific version
 */
final readonly class Snapshot implements SnapshotInterface
{
    public function __construct(
        private AggregateId $aggregateId,
        private int $version,
        private array $state,
        private string $aggregateClass,
        private Timestamp $createdAt
    ) {
    }

    public static function take(
        AggregateId $aggregateId,
        int $version,
        array $state,
        string $aggregateClass
    ): self {
        return new self(
            aggregateId: $aggregateId,
            version: $version,
            state: $state,
            aggregateClass: $aggregateClass,
            createdAt: Timestamp::now()
        );
    }

    public function getAggregateId(): AggregateId
    {
        return $this->aggregateId;
    }

    public function getVersion(): int
    {
        return $this->version;
    }

    public function getState(): array
    {
        return $this->state;
    }

    public function getCreatedAt(): Timestamp
    {
        return $this->createdAt;
    }

    public function getAggregateClass(): string
    {
        return $this->aggregateClass;
    }

    public function toArray(): array
    {
        return [
            'aggregate_id' => $this->aggregateId->toString(),
            'version' => $this->version,
            'state' => $this->state,
            'aggregate_class' => $this->aggregateClass,
            'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
        ];
    }
}
