<?php
/**
 * ITSWEBER CMS – Section Registry
 *
 * Zentrale Verwaltung aller verfügbaren Section-Typen.
 * Core-Sections werden beim Bootstrap registriert.
 * Drittanbieter-Plugins registrieren sich über den Hook 'sections.register'.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

namespace App\Sections;

use App\Core\Request;

class SectionRegistry
{
    /** @var array<string, AbstractSection> */
    private static array $sections = [];

    /**
     * Register all built-in core and extended sections,
     * then fire the hook for third-party plugins.
     */
    public static function registerCore(): void
    {
        // Core-Sections
        self::register(new Core\HeroSection());
        self::register(new Core\IntroSection());
        self::register(new Core\FeaturesSection());
        self::register(new Core\TopicCardsSection());
        self::register(new Core\HighlightGridSection());
        self::register(new Core\GalleryPreviewSection());
        self::register(new Core\BlogPreviewSection());
        self::register(new Core\EventsPreviewSection());
        self::register(new Core\CtaSection());

        // Extended (ehem. Plugins)
        self::register(new Extended\BeforeAfterSection());
        self::register(new Extended\FeatureShowcaseSection());
        self::register(new Extended\HtmlSection());
        self::register(new Extended\ProcessStepsSection());
        self::register(new Extended\RatingHighlightSection());
        self::register(new Extended\ServiceCardsSection());
        self::register(new Extended\SiteNoticeSection());
        self::register(new Extended\SocialEmbedSection());
        self::register(new Extended\TeamSection());
        self::register(new Extended\StatsSection());
        self::register(new Extended\TestimonialsSection());
        self::register(new Extended\TrustShowcaseSection());
        self::register(new Extended\VideoSection());

        // Theme-bound sections: loaded dynamically from active theme's bootstrap.php
        self::registerThemeSections();

        // Drittanbieter-Plugins können hier eigene Sections registrieren
        run_hook('sections.register');
    }

    /**
     * Load theme-bound sections from all themes that provide a bootstrap.php.
     * Each theme's bootstrap.php calls SectionRegistry::register() for its sections.
     * Sections are filtered by active theme in byCategory() – all are registered,
     * but only the active theme's sections appear in the "add section" modal.
     */
    public static function registerThemeSections(): void
    {
        $themesDir = dirname(__DIR__, 2) . '/themes';
        if (!is_dir($themesDir)) return;

        $dirs = scandir($themesDir);
        if ($dirs === false) return;

        foreach ($dirs as $slug) {
            if ($slug[0] === '.') continue;
            $bootstrap = $themesDir . '/' . $slug . '/bootstrap.php';
            if (is_file($bootstrap)) {
                require_once $bootstrap;
            }
        }
    }

    public static function register(AbstractSection $section): void
    {
        self::$sections[$section::typeKey()] = $section;
    }

    public static function get(string $typeKey): ?AbstractSection
    {
        return self::$sections[$typeKey] ?? null;
    }

    /** @return array<string, AbstractSection> */
    public static function all(): array
    {
        return self::$sections;
    }

    /**
     * Gruppiert nach Kategorie für das "Section hinzufügen" Modal.
     * Theme-bound sections are only included when their theme is active.
     */
    public static function byCategory(?string $activeTheme = null): array
    {
        $grouped = [];
        foreach (self::$sections as $section) {
            $theme = $section::themeSlug();
            if ($theme !== null && $theme !== $activeTheme) {
                continue;
            }
            $cat = $section::category();
            $grouped[$cat][] = $section;
        }
        return $grouped;
    }

    /** Daten-Validierung delegiert an die jeweilige Section-Klasse */
    public static function buildData(string $typeKey, Request $request): ?array
    {
        $section = self::get($typeKey);
        if (!$section) {
            return null;
        }
        return $section->buildData($request);
    }
}
