<?php
namespace BLR\Backline\Core;

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

final class Plugin {
    private static $instance = null;

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

    public function boot(): void {
        self::maybe_upgrade();

        SettingsPage::boot();
        AdminMenu::boot();
        Access::boot();
        Assets::boot();
        Privacy::boot();
        Media::boot();

        self::register_core_modules();
        \BLR\Backline\Modules\Users\Activity::boot();

        /**
         * Let add-on plugins register their own Backline modules.
         *
         * This runs after core modules are loaded so extensions can depend on
         * Backline internals without replacing core behavior.
         */
        do_action('blr_backline_register_modules');

        require_once BLR_BA_DIR . 'app/Core/profile-handler.php';
        ProfileHandler::boot();

        Router::boot();
        if (SettingsPage::is_module_enabled('outreach')) {
            \BLR\Backline\Modules\Outreach\Module::ensure_cron_infrastructure();
        }

        /**
         * Fires after Backline has finished booting.
         */
        do_action('blr_backline_booted');
    }


    private static function register_core_modules(): void {
        require_once BLR_BA_DIR . 'app/Modules/Dashboard/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Discography/module.php';
        require_once BLR_BA_DIR . 'app/Modules/Discography/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Events/module.php';
        require_once BLR_BA_DIR . 'app/Modules/Events/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Contacts/NewsletterDoubleOptIn.php';
        require_once BLR_BA_DIR . 'app/Modules/Contacts/contacts.php';
        require_once BLR_BA_DIR . 'app/Modules/Contacts/FanSignup.php';
        require_once BLR_BA_DIR . 'app/Modules/Contacts/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Outreach/module.php';
        require_once BLR_BA_DIR . 'app/Modules/Outreach/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Tasks/module.php';
        require_once BLR_BA_DIR . 'app/Modules/Tasks/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Users/Activity.php';
        require_once BLR_BA_DIR . 'app/Modules/Users/Pages.php';
        require_once BLR_BA_DIR . 'app/Modules/Notifications/module.php';
        require_once BLR_BA_DIR . 'app/Modules/Finances/Module.php';
        require_once BLR_BA_DIR . 'app/Modules/Finances/Pages.php';

        if (SettingsPage::is_module_enabled('discography')) {
            \BLR\Backline\Modules\Discography\Module::register_feature_only();
        }
        if (SettingsPage::is_module_enabled('events')) {
            \BLR\Backline\Modules\Events\Module::register_feature_only();
        }
        if (SettingsPage::is_module_enabled('contacts')) {
            \BLR\Backline\Modules\Contacts\Contacts::register();
            \BLR\Backline\Modules\Contacts\FanSignup::register();
        }
        if (SettingsPage::is_module_enabled('outreach')) {
            \BLR\Backline\Modules\Outreach\Module::register_feature_only();
        }
        if (SettingsPage::is_module_enabled('tasks')) {
            \BLR\Backline\Modules\Tasks\Module::register_feature_only();
        }
        if (SettingsPage::is_module_enabled('notifications')) {
            \BLR\Backline\Modules\Notifications\Module::register_feature_only();
        }
        if (SettingsPage::is_module_enabled('finances')) {
            \BLR\Backline\Modules\Finances\Module::register_feature_only();
        }

    }

    private static function maybe_upgrade(): void {
        $installed = (string) get_option(SettingsPage::OPT_PLUGIN_VERSION, '');
        if ($installed === BLR_BA_VERSION) {
            return;
        }

        SettingsPage::maybe_migrate_legacy_options();
        require_once BLR_BA_DIR . 'app/Modules/Notifications/module.php';
        \BLR\Backline\Modules\Notifications\Module::install();
        Access::grant_administrator_caps();
        update_option(SettingsPage::OPT_PLUGIN_VERSION, BLR_BA_VERSION, false);
    }

    public function url(string $path = ''): string {
        $path = ltrim($path, '/');
        return BLR_BA_URL . $path;
    }
}
