<?php

declare(strict_types=1);

namespace Apexl\Io\Services;

use Apexl\Io\Dtos\EntitySchema;

/**
 * One EntitySchema per table, per request.
 *
 * Keyed by table rather than by entity class: the shape belongs to the table, so two entities
 * over the same table describe it once between them, and a fixture whose table name changes
 * at runtime gets a fresh schema rather than a stale one.
 */
final class EntitySchemaRegistry
{
    /** @var array<string, EntitySchema> */
    private array $schemas = [];

    /** @param callable(): array<string, \stdClass> $describe */
    public function for(string $table, callable $describe): EntitySchema
    {
        return $this->schemas[$table] ??= EntitySchema::fromColumns($describe());
    }

    public function forget(string $table): void
    {
        unset($this->schemas[$table]);
    }
}
