<?php
/**
 * Core bootstrap class for the Priizmo AI Summary & FAQ Schema plugin.
 *
 * @package Priizmo_Engine
 */

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

/**
 * Class Priizmo_Engine
 *
 * Singleton responsible for wiring up the plugin's moving parts. Future
 * phases (post analysis, AI generation, schema output, etc.) will hook
 * into this class.
 */
class Priizmo_Engine {

	/**
	 * Single instance of this class.
	 *
	 * @var Priizmo_Engine|null
	 */
	private static $instance = null;

	/**
	 * Settings page controller.
	 *
	 * @var Priizmo_Engine_Settings
	 */
	public $settings;

	/**
	 * Post meta registration controller.
	 *
	 * @var Priizmo_Engine_Post_Meta
	 */
	public $post_meta;

	/**
	 * REST API controller.
	 *
	 * @var Priizmo_Engine_Rest
	 */
	public $rest;

	/**
	 * Block editor integration controller.
	 *
	 * @var Priizmo_Engine_Editor
	 */
	public $editor;

	/**
	 * Front-end display controller (shortcode, auto-insert, custom CSS).
	 *
	 * @var Priizmo_Engine_Display
	 */
	public $display;

	/**
	 * Bulk processing engine controller (Action Scheduler integration).
	 * Pro-only: null when running the free codebase, where the class
	 * doesn't exist at all (see priizmo-ai-summary-faq-schema.php).
	 *
	 * @var Priizmo_Engine_Bulk|null
	 */
	public $bulk;

	/**
	 * Retrieve (and lazily create) the singleton instance.
	 *
	 * @return Priizmo_Engine
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor is private to enforce the singleton pattern.
	 */
	private function __construct() {
		$this->settings  = new Priizmo_Engine_Settings();
		$this->post_meta = new Priizmo_Engine_Post_Meta();
		$this->rest      = new Priizmo_Engine_Rest();
		$this->editor    = new Priizmo_Engine_Editor();
		$this->display   = new Priizmo_Engine_Display();
		$this->bulk      = class_exists( 'Priizmo_Engine_Bulk' ) ? new Priizmo_Engine_Bulk() : null;

		// No manual load_plugin_textdomain() call: discouraged since WP 4.6.
		// WordPress.org automatically loads translations for plugins hosted
		// there, keyed off the "Text Domain" header in the main plugin file,
		// with no code required here.
	}

	/**
	 * Convenience accessor for a single setting value.
	 *
	 * @param string $key     Setting key.
	 * @param mixed  $default Fallback value if the setting is not set.
	 *
	 * @return mixed
	 */
	public static function get_setting( $key, $default = '' ) {
		$settings = get_option( PRIIZMO_ENGINE_OPTION_NAME, array() );

		return isset( $settings[ $key ] ) ? $settings[ $key ] : $default;
	}
}
