<?php

declare(strict_types=1);

namespace App\Framework\Notification\Templates;

/**
 * In-Memory Template Registry
 *
 * Simple in-memory implementation for template storage
 */
final class InMemoryTemplateRegistry implements TemplateRegistry
{
    /**
     * @var array<string, NotificationTemplate> Templates indexed by name
     */
    private array $templates = [];

    /**
     * @var array<string, NotificationTemplate> Templates indexed by ID
     */
    private array $templatesById = [];

    public function register(NotificationTemplate $template): void
    {
        $this->templates[$template->name] = $template;
        $this->templatesById[$template->id->toString()] = $template;
    }

    public function get(string $name): ?NotificationTemplate
    {
        return $this->templates[$name] ?? null;
    }

    public function getById(TemplateId $id): ?NotificationTemplate
    {
        return $this->templatesById[$id->toString()] ?? null;
    }

    public function has(string $name): bool
    {
        return isset($this->templates[$name]);
    }

    public function all(): array
    {
        return $this->templates;
    }

    public function remove(string $name): void
    {
        if (isset($this->templates[$name])) {
            $template = $this->templates[$name];
            unset($this->templates[$name]);
            unset($this->templatesById[$template->id->toString()]);
        }
    }
}
