<?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\TrustShowcaseSection());
        self::register(new Extended\VideoSection());

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

    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 */
    public static function byCategory(): array
    {
        $grouped = [];
        foreach (self::$sections as $section) {
            $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);
    }
}
