<?php
/**
 * KooKoa Folders
 *
 * @package KookoaWPF
 * @license GPL-2.0-or-later
 */

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

/**
 * AJAX dispatch surface for all folder operations.
 *
 * Owns the wp_ajax_kookoawpf_* action map (registered in the constructor) and
 * delegates every handler to the matching domain collaborator
 * (KookoaWPF_Ajax_Folders, _Meta, _Tree, _Items, _Settings, _Dynamic,
 * _Migration), each of which extends KookoaWPF_Ajax_Handler for the shared
 * nonce/capability and post-type guards. Keeping the action names and these
 * public method signatures here means external references and the registered
 * callbacks never change.
 *
 * @since 1.0.0
 */
class KookoaWPF_Ajax {

	/**
	 * Singleton instance.
	 *
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * Return the singleton instance.
	 *
	 * @return self
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Register all wp_ajax_ actions.
	 */
	private function __construct() {
		// Folder CRUD.
		add_action( 'wp_ajax_kookoawpf_create_folder', array( $this, 'create_folder' ) );
		add_action( 'wp_ajax_kookoawpf_rename_folder', array( $this, 'rename_folder' ) );
		add_action( 'wp_ajax_kookoawpf_delete_folder', array( $this, 'delete_folder' ) );
		add_action( 'wp_ajax_kookoawpf_move_folder', array( $this, 'move_folder' ) );
		add_action( 'wp_ajax_kookoawpf_get_tree', array( $this, 'get_tree' ) );
		// Note: reorder_folders removed in v3.5.0 — ordering is handled via kookoawpf_move_folder with sibling_order.

		// Item management.
		add_action( 'wp_ajax_kookoawpf_move_items', array( $this, 'move_items' ) );
		add_action( 'wp_ajax_kookoawpf_assign_items', array( $this, 'assign_items' ) );
		add_action( 'wp_ajax_kookoawpf_unassign_items', array( $this, 'unassign_items' ) );

		// Folder meta.
		add_action( 'wp_ajax_kookoawpf_update_folder_color', array( $this, 'update_folder_color' ) );
		add_action( 'wp_ajax_kookoawpf_toggle_lock', array( $this, 'toggle_lock' ) );
		add_action( 'wp_ajax_kookoawpf_toggle_pin', array( $this, 'toggle_pin' ) );
		add_action( 'wp_ajax_kookoawpf_duplicate_folder', array( $this, 'duplicate_folder' ) );
		add_action( 'wp_ajax_kookoawpf_update_folder_icon', array( $this, 'update_folder_icon' ) );

		// Settings.
		add_action( 'wp_ajax_kookoawpf_save_settings', array( $this, 'save_settings' ) );
		add_action( 'wp_ajax_kookoawpf_save_sidebar_width', array( $this, 'save_sidebar_width' ) );
		add_action( 'wp_ajax_kookoawpf_save_last_folder', array( $this, 'save_last_folder' ) );
		add_action( 'wp_ajax_kookoawpf_dismiss_review', array( $this, 'dismiss_review' ) );
		add_action( 'wp_ajax_kookoawpf_mark_review_clicked', array( $this, 'mark_review_clicked' ) );
		add_action( 'wp_ajax_kookoawpf_dismiss_notice', array( $this, 'dismiss_notice' ) );

		// Migration importers (Import & Export tab).
		add_action( 'wp_ajax_kookoawpf_run_importer', array( $this, 'run_importer' ) );
		add_action( 'wp_ajax_kookoawpf_dismiss_importer', array( $this, 'dismiss_importer' ) );
		add_action( 'wp_ajax_kookoawpf_reset_importers', array( $this, 'reset_importers' ) );
		add_action( 'wp_ajax_kookoawpf_hide_migrate_section', array( $this, 'hide_migrate_section' ) );
		add_action( 'wp_ajax_kookoawpf_dismiss_sidebar_migrate_hint', array( $this, 'dismiss_sidebar_migrate_hint' ) );
		add_action( 'wp_ajax_kookoawpf_deactivate_source_plugin', array( $this, 'deactivate_source_plugin' ) );

		// Sort.
		add_action( 'wp_ajax_kookoawpf_sort_folders', array( $this, 'sort_folders' ) );

		// Folder list (flat) for dropdowns.
		add_action( 'wp_ajax_kookoawpf_get_folder_list', array( $this, 'get_folder_list' ) );

		// Smart Folders (dynamic rules).
		add_action( 'wp_ajax_kookoawpf_create_dynamic_folder', array( $this, 'create_dynamic_folder' ) );
		add_action( 'wp_ajax_kookoawpf_update_dynamic_rules', array( $this, 'update_dynamic_rules' ) );
		add_action( 'wp_ajax_kookoawpf_get_dynamic_rules', array( $this, 'get_dynamic_rules' ) );

		// Export/Import — moved to class-kookoawpf-export.php.
	}

	/**
	 * AJAX: create a folder. Delegates to the Folders handler.
	 */
	public function create_folder(): void {
		KookoaWPF_Ajax_Folders::instance()->create_folder();
	}

	/**
	 * AJAX: rename a folder. Delegates to the Folders handler.
	 */
	public function rename_folder(): void {
		KookoaWPF_Ajax_Folders::instance()->rename_folder();
	}

	/**
	 * AJAX: delete a folder. Delegates to the Folders handler.
	 */
	public function delete_folder(): void {
		KookoaWPF_Ajax_Folders::instance()->delete_folder();
	}

	/**
	 * AJAX: move a folder to a new parent. Delegates to the Folders handler.
	 */
	public function move_folder(): void {
		KookoaWPF_Ajax_Folders::instance()->move_folder();
	}

	/**
	 * AJAX: return the folder tree + counts. Delegates to the Tree handler.
	 */
	public function get_tree(): void {
		KookoaWPF_Ajax_Tree::instance()->get_tree();
	}

	/**
	 * AJAX: move items between folders. Delegates to the Items handler.
	 */
	public function move_items(): void {
		KookoaWPF_Ajax_Items::instance()->move_items();
	}

	/**
	 * AJAX: assign items to a folder. Delegates to the Items handler.
	 */
	public function assign_items(): void {
		KookoaWPF_Ajax_Items::instance()->assign_items();
	}

	/**
	 * AJAX: unassign items from a folder. Delegates to the Items handler.
	 */
	public function unassign_items(): void {
		KookoaWPF_Ajax_Items::instance()->unassign_items();
	}

	/**
	 * AJAX: update a folder's colour. Delegates to the Meta handler.
	 */
	public function update_folder_color(): void {
		KookoaWPF_Ajax_Meta::instance()->update_folder_color();
	}

	/**
	 * AJAX: update a folder's icon. Delegates to the Meta handler.
	 */
	public function update_folder_icon(): void {
		KookoaWPF_Ajax_Meta::instance()->update_folder_icon();
	}

	/**
	 * AJAX: toggle a folder's lock state. Delegates to the Meta handler.
	 */
	public function toggle_lock(): void {
		KookoaWPF_Ajax_Meta::instance()->toggle_lock();
	}

	/**
	 * AJAX: toggle a folder's pin state. Delegates to the Meta handler.
	 */
	public function toggle_pin(): void {
		KookoaWPF_Ajax_Meta::instance()->toggle_pin();
	}

	/**
	 * AJAX: duplicate a folder and its subtree. Delegates to the Folders handler.
	 */
	public function duplicate_folder(): void {
		KookoaWPF_Ajax_Folders::instance()->duplicate_folder();
	}

	/**
	 * AJAX: persist the folder sort preference. Delegates to the Tree handler.
	 */
	public function sort_folders(): void {
		KookoaWPF_Ajax_Tree::instance()->sort_folders();
	}

	/**
	 * AJAX: return the flat folder list for dropdowns. Delegates to the Tree handler.
	 */
	public function get_folder_list(): void {
		KookoaWPF_Ajax_Tree::instance()->get_folder_list();
	}

	/**
	 * AJAX: save plugin settings. Delegates to the Settings handler.
	 */
	public function save_settings(): void {
		KookoaWPF_Ajax_Settings::instance()->save_settings();
	}

	/**
	 * AJAX: save the per-user sidebar width. Delegates to the Settings handler.
	 */
	public function save_sidebar_width(): void {
		KookoaWPF_Ajax_Settings::instance()->save_sidebar_width();
	}

	/**
	 * AJAX: save the per-user last-opened folder. Delegates to the Settings handler.
	 */
	public function save_last_folder(): void {
		KookoaWPF_Ajax_Settings::instance()->save_last_folder();
	}

	/**
	 * AJAX: dismiss the Rate Us card. Delegates to the Settings handler.
	 */
	public function dismiss_review(): void {
		KookoaWPF_Ajax_Settings::instance()->dismiss_review();
	}

	/**
	 * AJAX: mark the review link as clicked. Delegates to the Settings handler.
	 */
	public function mark_review_clicked(): void {
		KookoaWPF_Ajax_Settings::instance()->mark_review_clicked();
	}

	/**
	 * AJAX: dismiss an admin notice. Delegates to the Settings handler.
	 */
	public function dismiss_notice(): void {
		KookoaWPF_Ajax_Settings::instance()->dismiss_notice();
	}

	/**
	 * AJAX: create a smart folder with rules. Delegates to the Dynamic handler.
	 */
	public function create_dynamic_folder(): void {
		KookoaWPF_Ajax_Dynamic::instance()->create_dynamic_folder();
	}

	/**
	 * AJAX: update a smart folder's rules. Delegates to the Dynamic handler.
	 */
	public function update_dynamic_rules(): void {
		KookoaWPF_Ajax_Dynamic::instance()->update_dynamic_rules();
	}

	/**
	 * AJAX: read a smart folder's rules. Delegates to the Dynamic handler.
	 */
	public function get_dynamic_rules(): void {
		KookoaWPF_Ajax_Dynamic::instance()->get_dynamic_rules();
	}

	/**
	 * AJAX: run a migration importer. Delegates to the Migration handler.
	 */
	public function run_importer(): void {
		KookoaWPF_Ajax_Migration::instance()->run_importer();
	}

	/**
	 * AJAX: dismiss an importer card. Delegates to the Migration handler.
	 */
	public function dismiss_importer(): void {
		KookoaWPF_Ajax_Migration::instance()->dismiss_importer();
	}

	/**
	 * AJAX: reset importer detection flags. Delegates to the Migration handler.
	 */
	public function reset_importers(): void {
		KookoaWPF_Ajax_Migration::instance()->reset_importers();
	}

	/**
	 * AJAX: hide the migrate section permanently. Delegates to the Migration handler.
	 */
	public function hide_migrate_section(): void {
		KookoaWPF_Ajax_Migration::instance()->hide_migrate_section();
	}

	/**
	 * AJAX: dismiss the sidebar migrate hint. Delegates to the Migration handler.
	 */
	public function dismiss_sidebar_migrate_hint(): void {
		KookoaWPF_Ajax_Migration::instance()->dismiss_sidebar_migrate_hint();
	}

	/**
	 * AJAX: deactivate a source plugin after migration. Delegates to the Migration handler.
	 */
	public function deactivate_source_plugin(): void {
		KookoaWPF_Ajax_Migration::instance()->deactivate_source_plugin();
	}
}
