<?php
/**
 * Shipment Hooks
 *
 * Registers hooks for shipment confirmation functionality.
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/hooks
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Hooks;

use MOMCS_Sync\Includes\Common\Controllers\Shipment_Controller;

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

/**
 * Class for registering shipment-related hooks.
 */
class Shipment_Hooks {

	/**
	 * Shipment Controller instance.
	 *
	 * @var Shipment_Controller
	 */
	private $controller;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->controller = new Shipment_Controller();
		$this->register_hooks();
	}

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public function register_hooks(): void {
		// Register AJAX endpoint for shipment confirmation.
		add_action( 'wp_ajax_momcs_confirm_amazon_shipment', array( $this->controller, 'confirm_amazon_shipment' ) );

		// Register meta box on order edit screen.
		add_action( 'add_meta_boxes', array( $this, 'register_shipment_meta_box' ) );
	}

	/**
	 * Register shipment confirmation meta box.
	 *
	 * @return void
	 */
	public function register_shipment_meta_box(): void {
		// Check if we're on the order edit screen (support both HPOS and legacy).
		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		// Support both HPOS and legacy order screens.
		$is_order_screen = in_array( $screen->id, array( 'shop_order', 'woocommerce_page_wc-orders' ), true );
		if ( ! $is_order_screen ) {
			return;
		}

		// Get order ID based on screen type.
		$order_id = null;
		if ( 'woocommerce_page_wc-orders' === $screen->id ) {
			// HPOS: Get ID from query string.
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No action is being performed.
			$order_id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0;
		} elseif ( 'shop_order' === $screen->id ) {
			// Legacy: Get ID from post.
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No action is being performed.
			$order_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
		}

		if ( ! $order_id ) {
			return;
		}

		$order = wc_get_order( $order_id );
		if ( ! $order ) {
			return;
		}

		$amazon_order_id = $order->get_meta( '_amazon_order_id' );
		if ( empty( $amazon_order_id ) ) {
			return;
		}

		// Register meta box for both screen types.
		add_meta_box(
			'momcs_amazon_shipment',
			__( 'Amazon Shipment Confirmation', 'multi-channel-for-woocommerce' ),
			array( $this, 'render_shipment_meta_box' ),
			array( 'shop_order', 'woocommerce_page_wc-orders' ),
			'normal',
			'default'
		);
	}

	/**
	 * Render shipment confirmation meta box.
	 *
	 * @param \WP_Post|\WC_Order $post_or_order_object The post or order object.
	 * @return void
	 */
	public function render_shipment_meta_box( $post_or_order_object ): void {
		// Get order ID from either post or order object (HPOS compatibility).
		if ( $post_or_order_object instanceof \WC_Order ) {
			$order_id = $post_or_order_object->get_id();
		} elseif ( $post_or_order_object instanceof \WP_Post ) {
			$order_id = $post_or_order_object->ID;
		} else {
			return;
		}

		// Load and render the meta box view.
		$view = new \MOMCS_Sync\Views\Src\Common\Pages\Orders\Amazon_Shipment_Metabox_View();
		$view->render( $order_id );
	}
}
