<?php

declare( strict_types=1 );

namespace Brightfern;

use Brightfern\Admin\SettingsPage;
use Brightfern\Inbound\InboundInbox;
use Brightfern\Inbound\InboundStore;
use Brightfern\Inbound\WebhookController;
use Brightfern\WooCommerce\OrderMetabox;
use Brightfern\WooCommerce\OutboundFlow;

/**
 * Composition root. Instantiated once on `plugins_loaded` (after WooCommerce is
 * confirmed) and wires the modules together. Each subsystem registers its own
 * hooks; this class just owns their lifecycle.
 */
final class Plugin {

	public function boot(): void {
		// Make sure the inbound table exists even if the plugin was updated
		// (not freshly activated). Cheap dbDelta no-op when already current.
		$this->ensureSchema();

		// Outbound SEND path (Free: generate+validate+download / Pro: transmit).
		( new OutboundFlow() )->register();

		// Inbound RECEIVE path (free): REST webhook + polling fallback.
		( new WebhookController() )->register();

		// Admin surfaces.
		if ( is_admin() ) {
			( new SettingsPage() )->register();
			( new OrderMetabox() )->register();
			( new InboundInbox() )->register();
		}
	}

	private function ensureSchema(): void {
		$installed = get_option( 'brightfern_db_version' );
		if ( BRIGHTFERN_VERSION !== $installed ) {
			InboundStore::install();
			update_option( 'brightfern_db_version', BRIGHTFERN_VERSION );
		}
	}
}
