<?php
/**
 * Main plugin orchestrator (singleton).
 *
 * @package Brainwerk Security Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

final class Shieldforge {

    private static $instance = null;

    /** @var Shieldforge_Loader */
    public $loader;

    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->loader = new Shieldforge_Loader();
        $this->set_locale();
        $this->load_modules();
    }

    private function set_locale() {
        // Bundled-translation loading + the admin language override live in
        // Shieldforge_I18n (registered in load_modules). Here we only keep
        // the de_AT/de_CH/de_DE_formal fallback filter so the German
        // variants find brainwerk-security-suite-de_DE.mo.
        add_filter( 'load_textdomain_mofile', array( $this, 'fallback_de_variants' ), 10, 2 );
    }

    /**
     * If WordPress is asking for our shieldforge-de_AT.mo (or de_CH /
     * de_DE_formal) and that file doesn't exist, redirect the lookup to
     * shieldforge-de_DE.mo. Lets the Schweizer / Österreicher / formal
     * crowd see German without us shipping three near-identical copies.
     */
    public function fallback_de_variants( $mofile, $domain ) {
        // Cover both Free ('brainwerk-security-suite') and Pro ('brainwerk-security-suite-premium') so the
        // Pro plugin doesn't need to register its own filter.
        if ( 'brainwerk-security-suite' !== $domain && 'brainwerk-security-suite-premium' !== $domain ) {
            return $mofile;
        }
        $regionals = array( 'de_AT', 'de_CH', 'de_DE_formal' );
        foreach ( $regionals as $loc ) {
            if ( false !== strpos( $mofile, '-' . $loc . '.mo' ) && ! file_exists( $mofile ) ) {
                $alt = str_replace( '-' . $loc . '.mo', '-de_DE.mo', $mofile );
                if ( file_exists( $alt ) ) {
                    return $alt;
                }
            }
        }
        return $mofile;
    }

    /**
     * Wires modules into the loader.
     *
     * Each module is a class in includes/ that registers its own hooks
     * via the loader.
     */
    private function load_modules() {
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-utils.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-settings.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-logger.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-mem-handle.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-geo.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-asn.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-cve-rules.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-event.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-bruteforce.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-hardening.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-probing.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-notifications.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-threat-intel.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-tools.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-multisite.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-anomaly.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-privacy-generator.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-onboarding-i18n.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-i18n.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-onboarding.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-ajax.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-pro.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-healthcheck.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-migrations.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-scanner.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-pattern-scanner.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-totp.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-twofactor.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-twofactor-profile.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-vulnscanner.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-auditlog.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-login-url.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-honeypot.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-quick-setup.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-rest.php';
        // v0.12 — Konkurrenz-Catch-Up + USP-Ausbau
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-pwned.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-headers.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-captcha.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-live-traffic.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-core-compare.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-privacy-dashboard.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-trusted-devices.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-magic-link.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-waf.php';
        // v0.15 — Wordfence-Catch-Up: Repo-Integrität + Crawler-Rate-Limiting
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-repo-integrity.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-rate-limiter.php';
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-badge.php';
        // Pfad B — Threat-Intelligence Network sensor (opt-in): collect, ship, apply the signed feed
        require_once SHIELDFORGE_PATH . 'includes/sensor/class-shieldforge-sensor.php';
        // Phase 7 Stage 3 — Incident Commander (Post-Incident Forensics & Lockdown)
        require_once SHIELDFORGE_PATH . 'includes/class-shieldforge-incident.php';

        Shieldforge_I18n::register( $this->loader );
        Shieldforge_Pro::register( $this->loader );
        Shieldforge_Healthcheck::register( $this->loader );
        Shieldforge_Migrations::register( $this->loader );
        Shieldforge_Logger::register( $this->loader );
        Shieldforge_Bruteforce::register( $this->loader );
        Shieldforge_Hardening::register( $this->loader );
        Shieldforge_Probing::register( $this->loader );
        Shieldforge_Notifications::register( $this->loader );
        Shieldforge_Tools::register( $this->loader );
        Shieldforge_Multisite::register( $this->loader );
        Shieldforge_Anomaly::register( $this->loader );
        Shieldforge_Privacy_Generator::register( $this->loader );
        Shieldforge_Onboarding::register( $this->loader );
        Shieldforge_Ajax::register( $this->loader );
        Shieldforge_Scanner::register( $this->loader );
        Shieldforge_TwoFactor::register( $this->loader );
        Shieldforge_TwoFactor_Profile::register( $this->loader );
        Shieldforge_VulnScanner::register( $this->loader );
        Shieldforge_AuditLog::register( $this->loader );
        Shieldforge_Quick_Setup::register( $this->loader );
        Shieldforge_Login_Url::register( $this->loader );
        Shieldforge_Honeypot::register( $this->loader );
        Shieldforge_Rest::register( $this->loader );
        // v0.12 — Module registrieren (alle OPT-IN, default off)
        Shieldforge_Pwned::register( $this->loader );
        Shieldforge_Headers::register( $this->loader );
        Shieldforge_Captcha::register( $this->loader );
        Shieldforge_Live_Traffic::register( $this->loader );
        Shieldforge_Core_Compare::register( $this->loader );
        Shieldforge_Privacy_Dashboard::register( $this->loader );
        Shieldforge_Trusted_Devices::register( $this->loader );
        Shieldforge_Magic_Link::register( $this->loader );
        Shieldforge_Waf::register( $this->loader );
        Shieldforge_Repo_Integrity::register( $this->loader );
        Shieldforge_Rate_Limiter::register( $this->loader );
        Shieldforge_Badge::register( $this->loader );
        Shieldforge_Sensor::register( $this->loader );
        Shieldforge_Incident::register( $this->loader );

        if ( is_admin() ) {
            require_once SHIELDFORGE_PATH . 'admin/class-shieldforge-admin.php';
            require_once SHIELDFORGE_PATH . 'admin/class-shieldforge-dashboard-widget.php';
            Shieldforge_Admin::register( $this->loader );
            Shieldforge_Dashboard_Widget::register( $this->loader );
        }
    }

    public function run() {
        $this->loader->run();
    }
}
