<?php

/**
 * SocialPromoter Core
 *
 * Bootstraps all plugin components on plugins_loaded. Owns the deactivation
 * hook and the DB upgrade check.
 *
 * @package PublisherKits_SocialPromoter
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Class PKITS_SP_Core
 */
class PKITS_SP_Core {
    /**
     * Plugin singleton instance.
     *
     * @var PKITS_SP_Core|null
     */
    private static $instance = null;

    /**
     * Admin handler instance.
     *
     * @var PKITS_SP_Admin
     */
    private $admin;

    /**
     * Metabox handler instance.
     *
     * @var PKITS_SP_Metabox
     */
    private $metabox;

    /**
     * Queue handler instance.
     *
     * @var PKITS_SP_Queue
     */
    private $queue;

    /**
     * Nudge handler instance.
     *
     * @var PKITS_SP_Nudge
     */
    private $nudge;

    /**
     * Private constructor — use ::init().
     */
    private function __construct() {
        PKITS_SP_Installer::maybe_upgrade();
        // Register platform connectors.
        PKITS_SP_Platform_Registry::register( new PKITS_SP_Connector_X() );
        PKITS_SP_Platform_Registry::register( new PKITS_SP_Connector_Facebook() );
        PKITS_SP_Platform_Registry::register( new PKITS_SP_Connector_LinkedIn() );
        PKITS_SP_Platform_Registry::register( new PKITS_SP_Connector_Bluesky() );
        $this->admin = new PKITS_SP_Admin();
        $this->metabox = new PKITS_SP_Metabox();
        $this->queue = new PKITS_SP_Queue();
        $this->nudge = new PKITS_SP_Nudge();
        $this->register_hooks();
    }

    /**
     * Initialise the plugin. Called on plugins_loaded.
     *
     * @return PKITS_SP_Core
     */
    public static function init() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Plugin deactivation handler. Clears scheduled cron events.
     *
     * @return void
     */
    public static function deactivate() {
        // WP Cron events are cleared on deactivation. We do not remove the queue
        // table or options — data is preserved. Use wp_unschedule_hook: it removes
        // EVERY event for a hook regardless of args, so the per-item fire and boost
        // events (which carry a queue_id / post_id, and so are NOT matched by an
        // argless wp_clear_scheduled_hook) are actually cleared.
        wp_unschedule_hook( PKITS_SP_Queue::CRON_HOOK );
        // Stats are premium-only (class stripped from the free build); the hook
        // name is a literal so deactivation clears it on every build.
        wp_unschedule_hook( 'pkits_sp_refresh_stats' );
        // PKITS_SP_Automation is a premium-only class stripped from the free build,
        // so its hook names are referenced here as literals — deactivation runs on
        // the free build too and must not depend on the class being present.
        wp_unschedule_hook( 'pkits_sp_automation_tick' );
        // PKITS_SP_Automation::TICK_HOOK.
        wp_unschedule_hook( 'pkits_sp_run_boost' );
        // PKITS_SP_Automation::BOOST_HOOK.
    }

    /**
     * Register all WordPress hooks.
     *
     * @return void
     */
    private function register_hooks() {
        // Queue cron.
        $this->queue->register_hooks();
        // Nudge system.
        $this->nudge->register_hooks();
        // Admin.
        if ( is_admin() ) {
            add_action( 'admin_menu', array($this->admin, 'register_menus') );
            add_action( 'admin_enqueue_scripts', array($this->admin, 'enqueue_assets') );
            add_action( 'admin_notices', array($this->admin, 'show_admin_notice') );
            add_action( 'admin_init', array($this->admin, 'handle_oauth_callback') );
            // Form POST handlers.
            add_action( 'admin_post_pkits_sp_save_credentials', array($this->admin, 'handle_save_credentials') );
            add_action( 'admin_post_pkits_sp_linkedin_callback', array($this->admin, 'process_linkedin_relay_callback') );
            add_action( 'admin_post_pkits_sp_delete_account', array($this->admin, 'handle_delete_account') );
            add_action( 'admin_post_pkits_sp_bluesky_connect', array($this->admin, 'handle_bluesky_connect') );
            add_action( 'admin_post_pkits_sp_save_active_accounts', array($this->admin, 'handle_save_active_accounts') );
            add_action( 'admin_post_pkits_sp_cancel_queue_item', array($this->admin, 'handle_cancel_queue_item') );
            add_action( 'admin_post_pkits_sp_save_settings', array($this->admin, 'handle_save_settings') );
            // Metabox - registration on add_meta_boxes, AJAX on admin_init.
            add_action( 'add_meta_boxes', array($this->metabox, 'register') );
            add_action( 'admin_init', array($this->metabox, 'register_ajax_handlers') );
            add_action( 'admin_init', array($this->admin, 'register_compose_ajax_handlers') );
            add_action( 'admin_init', array($this->admin, 'register_post_search_ajax_handlers') );
            add_action( 'admin_init', array($this->admin, 'register_preview_ajax_handlers') );
        }
        // Translations: WordPress.org auto-loads them for the plugin slug since WP
        // 4.6, and this plugin requires 6.0+, so no load_plugin_textdomain() call
        // is needed.
    }

}
