<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class BingoCR {

	private static $instance = null;

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

	public function init(): void {
		// Translations load automatically: WordPress.org language packs and
		// bundled /languages files are picked up just-in-time (Domain Path is
		// set in the plugin header), so no load_plugin_textdomain() is needed.

		// BingoCR_REST_Premium extends BingoCR_REST_API and adds the extras/discount
		// CRUD + PayPal routes on top. It's physically absent from the free build
		// (see class-bingocr-rest-premium__premium_only.php), so those routes simply
		// don't exist there — not gated, absent.
		$bingocr_rest = class_exists( 'BingoCR_REST_Premium' ) ? new BingoCR_REST_Premium() : new BingoCR_REST_API();
		$bingocr_rest->register();
		( new BingoCR_Shortcode() )->register();

		if ( is_admin() ) {
			( new BingoCR_Admin() )->register();
		}

		// Upgrade DB tables if needed.
		add_action( 'admin_init', array( $this, 'maybe_upgrade' ) );
	}

	public function maybe_upgrade(): void {
		if ( get_option( 'bingocr_db_version' ) !== BINGOCR_DB_VERSION ) {
			BingoCR_Activator::activate();
		}
	}
}
