<?php
/**
 * Standalone Mode Hook Registration
 *
 * Registers WordPress and WooCommerce hooks specific to standalone mode.
 * Handles order list actions, meta boxes, and AJAX handlers for PDF generation.
 *
 * @package Freefactu
 * @since 0.5.0
 */

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

/**
 * Class Freefactu_Standalone_Hooks
 *
 * Manages hook registration for plugin operation.
 *
 * This class is responsible for:
 * - Registering order list action buttons (Verifactu icon)
 * - Adding meta boxes to single order pages
 * - Registering AJAX handlers for PDF operations
 * - Managing plugin-specific UI elements
 *
 * This class encapsulates all WordPress and WooCommerce hooks in one place.
 *
 * The class follows the Single Responsibility Principle: it only handles
 * hook registration. The actual functionality is implemented in other
 * classes (Admin, Client, Generator).
 *
 * @see Freefactu_Admin For the admin functionality implementation
 */
class Freefactu_Standalone_Hooks {

	/**
	 * Admin instance for accessing admin methods.
	 *
	 * @var Freefactu_Admin
	 */
	private $admin;

	/**
	 * Constructor.
	 *
	 * @param Freefactu_Admin $admin Admin instance.
	 */
	public function __construct( $admin ) {
		$this->admin = $admin;
		$this->register_hooks();
	}

	/**
	 * Register all standalone mode hooks.
	 *
	 * @return void
	 */
	private function register_hooks() {
		// Register order list actions
		add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_order_list_actions' ) );

		// Register meta box for single order view
		add_action( 'add_meta_boxes', array( $this, 'add_invoice_meta_box' ) );

		// Register generator's PDF output hooks
		$generator = Freefactu_Factory::create_generator();
		$generator->register_output_hooks();
	}

	/**
	 * Add standalone invoice action buttons to WooCommerce order list.
	 *
	 * Outputs the Verifactu icon HTML directly using the centralized
	 * render_verifactu_icon_html() function for consistent icon rendering.
	 *
	 * @param WC_Order $order The order object.
	 * @return void
	 */
	public function add_order_list_actions( $order ) {
		// Do not show buttons for trashed orders
		if ( 'trash' === $order->get_status() ) {
			return;
		}

		// Use centralized icon generation function.
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Function uses esc_attr/esc_url/esc_html for all dynamic values. See docblock in class-freefactu-admin.php.
		echo Freefactu_Admin::render_verifactu_icon_html( $order, 'order_list' );
	}


	/**
	 * Add meta box for standalone invoice actions on single order view.
	 *
	 * @return void
	 */
	public function add_invoice_meta_box() {
		$screen = wc_get_container()->get( \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
			? wc_get_page_screen_id( 'shop-order' )
			: 'shop_order';

		add_meta_box(
			'freefactu-invoice-actions',
			__( 'Factura Verifactu', 'freefactu' ),
			array( $this, 'render_invoice_meta_box' ),
			$screen,
			'side',
			'high'
		);
	}

	/**
	 * Render the standalone invoice meta box content.
	 *
	 * Uses the centralized render_verifactu_icon_html() function for consistent
	 * icon rendering across all display locations.
	 *
	 * @param WP_Post|WC_Order $post_or_order The post object or order object.
	 * @return void
	 */
	public function render_invoice_meta_box( $post_or_order ) {
		// Get order object - handle both WP_Post and WC_Order inputs
		if ( $post_or_order instanceof WC_Order ) {
			$order = $post_or_order;
		} else {
			$order = wc_get_order( $post_or_order->ID );
		}

		// Delegate to static method for actual rendering
		Freefactu_Admin::render_invoice_meta_box_content( $order );
	}
}
