<?php

declare( strict_types=1 );

namespace ConflictRadar\Core;

use ConflictRadar\Monitor\UpdateTracker;
use ConflictRadar\Monitor\ErrorLogReader;
use ConflictRadar\Admin\AdminMenu;
use ConflictRadar\Admin\DashboardWidget;
use ConflictRadar\Admin\Assets;

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

/**
 * Main orchestrator. Instantiated on plugins_loaded; registers all hooks.
 */
class Plugin {

	public function run(): void {
		// Monitors (front + back end).
		( new UpdateTracker() )->register();
		( new ErrorLogReader() )->register();

		// Admin UI (Phase 4) — only needed in the dashboard.
		if ( is_admin() ) {
			( new AdminMenu() )->register();
			( new DashboardWidget() )->register();
			( new Assets() )->register();
		}

		// Pro feature gate. The CONFLICT_RADAR_PRO_VERSION constant is defined on
		// plugins_loaded priority 1 (in conflict-radar.php) when Freemius reports
		// the premium build, so it is already set by the time run() executes at
		// priority 10. Always use defined() — never class_exists() (forbidden).
		if ( defined( 'CONFLICT_RADAR_PRO_VERSION' ) ) {
			// Pro self-provisions its premium tables, options, and digest cron on load
			// (cheap: gated behind a db-version option + add_option/wp_next_scheduled).
			\ConflictRadar\Pro\ProProvisioner::ensure();

			// Phase 6: error/update correlation. Registers the event seam + cron chain.
			( new \ConflictRadar\Pro\CorrelationEngine() )->register();

			// Phase 6: correlation ⚠ badge on the Update History table (via filter).
			( new \ConflictRadar\Pro\CorrelationBadge() )->register();

			// Phase 7: email alerts. Same two-layer gate — only wire when the premium
			// code can actually be used (active license).
			if ( function_exists( 'conflict_radar_fs' ) && \conflict_radar_fs()->can_use_premium_code() ) {
				( new \ConflictRadar\Pro\AlertManager() )->register();
			}
		}
	}
}
