<?php
/**
 * Core plugin orchestrator.
 *
 * @package Ppok\AIToolkit
 * @license GPL-2.0-or-later
 */

namespace Ppok\AIToolkit;

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

class Plugin {

	protected Loader $loader;
	protected string $plugin_name = 'ppok-ai-toolkit';
	protected string $version;

	public function __construct() {
		$this->version = defined( 'PPOK_AI_TOOLKIT_VERSION' ) ? PPOK_AI_TOOLKIT_VERSION : '0.2.1';
		$this->loader  = new Loader();

		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
		$this->define_abilities_hooks();
	}

	private function set_locale(): void {
		$i18n = new I18n();
		$this->loader->add_action( 'init', $i18n, 'load_plugin_textdomain' );
	}

	private function define_admin_hooks(): void {
		$admin = new \Ppok\AIToolkit\Admin\Admin( $this->plugin_name, $this->version );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_scripts' );
		$this->loader->add_action( 'admin_menu', $admin, 'register_settings_page' );
		$this->loader->add_action( 'admin_init', $admin, 'register_settings' );
		$this->loader->add_filter(
			'plugin_action_links_' . PPOK_AI_TOOLKIT_BASENAME,
			$admin,
			'plugin_action_links'
		);

		$dependency_notice = new \Ppok\AIToolkit\Admin\Dependency_Notice();
		$this->loader->add_action( 'admin_notices', $dependency_notice, 'maybe_render_notice' );
	}

	private function define_public_hooks(): void {
		$public = new \Ppok\AIToolkit\Front\Front( $this->plugin_name, $this->version );
		$this->loader->add_action( 'wp_enqueue_scripts', $public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $public, 'enqueue_scripts' );
	}

	private function define_abilities_hooks(): void {
		$context = new \Ppok\AIToolkit\Request_Context();
		\Ppok\AIToolkit\Request_Context::set_instance( $context );
		$this->loader->add_filter( 'rest_pre_dispatch', $context, 'on_rest_pre_dispatch', 10, 3 );

		$logger = new \Ppok\AIToolkit\Event_Logger( $context );
		$this->loader->add_action( 'admin_init', $logger, 'maybe_install_schema' );

		$registrar = new \Ppok\AIToolkit\Abilities\Ability_Registrar( $logger );
		$this->loader->add_action( 'wp_abilities_api_categories_init', $registrar, 'register_categories' );
		$this->loader->add_action( 'wp_abilities_api_init', $registrar, 'register_all' );

		// Front-end consumer of the ppok/preview-url ability — registers
		// the query vars and expands the main-query post_status filter
		// when a valid token is present. Token mint + token verify live
		// on the same class so they can't drift out of sync.
		$preview = new \Ppok\AIToolkit\Abilities\Preview_Url();
		$this->loader->add_filter( 'query_vars', $preview, 'register_query_vars' );
		$this->loader->add_action( 'pre_get_posts', $preview, 'maybe_allow_preview_query' );
	}

	public function run(): void {
		$this->loader->run();
	}

	public function get_plugin_name(): string {
		return $this->plugin_name;
	}

	public function get_version(): string {
		return $this->version;
	}

	public function get_loader(): Loader {
		return $this->loader;
	}
}
