<?php

namespace SoftminalB2B;

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

use Exception;
use SoftminalB2B\Enqueue;
use SoftminalB2B\Http\Routes\Router;
use SoftminalB2B\Menu;
use SoftminalB2B\Cart\BogoRewards;
use SoftminalB2B\Cart\QuantityRules;
use SoftminalB2B\Pricing\PriceFilters;
use SoftminalB2B\Registration\Emails;
use SoftminalB2B\Services\Pdf\Invoices;
use SoftminalB2B\Services\Scheduler;
use SoftminalB2B\Support\MailHealth;
use SoftminalB2B\Registration\RegistrationForm;
use SoftminalB2B\Storefront\BlockEditor;
use SoftminalB2B\Storefront\BulkOrderForm;
use SoftminalB2B\Storefront\PurchaseListsAccount;
use SoftminalB2B\Storefront\MessageCentre;
use SoftminalB2B\Storefront\PriceVisibility;
use SoftminalB2B\Storefront\PurchaseOrder;
use SoftminalB2B\Storefront\Reorder;
use SoftminalB2B\Storefront\TaxToggle;
use SoftminalB2B\Storefront\ViewAsCustomer;

class App
{
    private static ?self $instance = null;
    private Enqueue $enqueue;
    private Menu $menu;
    private Router $router;

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

    private function __construct()
    {
        $this->initializeClasses();
    }

    private function initializeClasses(): void
    {
        $this->enqueue = new Enqueue();
        $this->menu = new Menu();
        $this->router = new Router();
    }

    public function run(): void
    {
        $this->enqueue->init();
        $this->menu->init();
        $this->router->init();

        // Plugins-screen deactivation survey — independent of WooCommerce.
        DeactivationSurvey::init();

        // Requires WooCommerce; without it there are no prices to resolve.
        if (class_exists('WooCommerce')) {
            PriceFilters::init();
            PriceVisibility::init();
            QuantityRules::init();
            BogoRewards::init();
            ViewAsCustomer::init();
            RegistrationForm::init();
            Emails::init();
            BulkOrderForm::init();
            PurchaseListsAccount::init();
            MessageCentre::init();
            BlockEditor::init();
            Reorder::init();
            PurchaseOrder::init();
            TaxToggle::init();
            Invoices::init();
            Scheduler::init();
        }

        // Email deliverability guidance (Site Health) — the suite emails
        // invoices/approvals/quotes but never owns delivery.
        MailHealth::init();
    }
}
