<?php

declare(strict_types=1);

namespace App\Framework\Http\Session;

/**
 * Simple in-memory session storage for fallback when Redis is not available.
 * Warning: Data will be lost when process ends - only for development/fallback.
 */
final class InMemorySessionStorage implements SessionStorage
{
    /** @var array<string, array> */
    private array $sessions = [];

    public function read(SessionId $id): array
    {
        return $this->sessions[$id->toString()] ?? [];
    }

    public function write(SessionId $id, array $data): void
    {
        $this->sessions[$id->toString()] = $data;
    }

    public function remove(SessionId $id): void
    {
        unset($this->sessions[$id->toString()]);
    }

    public function migrate(SessionId $oldId, SessionId $newId): void
    {
        $data = $this->read($oldId);
        $this->write($newId, $data);
        $this->remove($oldId);
    }
}
