<?php
/**
 * Admin Meta Boxes for WooCommerce Orders.
 * 
 * @package ParcelSelector
 */

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

if ( ! class_exists( 'Parcselect_Order_Meta_Boxes' ) ) {
class Parcselect_Order_Meta_Boxes {

	public static function init() {
		add_action( 'add_meta_boxes', array( __CLASS__, 'register_meta_boxes' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
		// Legacy display fallback
		add_action( 'woocommerce_admin_order_data_after_shipping_address', array( __CLASS__, 'display_legacy_card' ), 10, 1 );
		// VPS Order Display Banner — shown at the very top of WC order admin page
	}

	public static function register_meta_boxes() {
		$t       = parcselect_admin_i18n();
		$screens = array( 'shop_order', 'woocommerce_page_wc-orders' );
		foreach ( $screens as $screen ) {
			// 1. Core Locker Details
			add_meta_box(
				'parcselect_inpost_details_box',
				$t['order_shipping_details'],
				array( __CLASS__, 'render_details_box' ),
				$screen,
				'side',
				'high'
			);

			// 2. NASZA WYŚWIETLAJKA (Server Notice)
			$notice = get_option( 'parcselect_server_notice', null );
			if ( ! empty( $notice ) && ! empty( $notice['text'] ) ) {
				add_meta_box(
					'ps-server-notice-metabox',
					'&#x1F4E6; ParcelSelector.com',
					array( __CLASS__, 'render_notice_box' ),
					$screen,
					'side',
					'high'
				);
			}
		}
	}

	public static function render_details_box( $post_or_order ) {
		$t     = parcselect_admin_i18n();
		$order = ( $post_or_order instanceof WC_Order ) ? $post_or_order : wc_get_order( is_object( $post_or_order ) ? $post_or_order->ID : $post_or_order );
		if ( ! $order ) return;

		$data = parcselect_get_order_data( $order );
		if ( ! $data['has_data'] ) {
			echo '<p>' . esc_html( $t['order_no_locker'] ) . '</p>';
			return;
		}

		if ( function_exists( 'parcselect_render_locker_card' ) ) {
			echo wp_kses_post( parcselect_render_locker_card( $data['paczkomat'], 'admin', $order ) );
		}
	}

	public static function render_notice_box() {
		if ( function_exists( 'parcselect_render_server_notice' ) ) {
			parcselect_render_server_notice( 'wc_order' );
		}
	}

	public static function display_legacy_card( $order ) {
		// Guard: ensure we have a proper WC_Order (not WC_Order_Refund etc.)
		if ( ! is_a( $order, 'WC_Order' ) ) {
			return;
		}
		// Only show for PS method
		$items = $order->get_items( 'shipping' );
		$is_ps = false;
		foreach ( $items as $item ) {
			if ( false !== strpos( $item->get_method_id(), 'parcselect_inpost_paczkomat' ) ) {
				$is_ps = true;
				break;
			}
		}
		if ( ! $is_ps ) return;

		$data = parcselect_get_order_data( $order );
		if ( $data['has_data'] && function_exists( 'parcselect_render_locker_card' ) ) {
			echo wp_kses_post( parcselect_render_locker_card( $data['paczkomat'], 'admin', $order ) );
		}
	}

	/**
	 * Enqueue scripts and styles for the order edit page.
	 */
	public static function enqueue_assets( $hook ) {
		// Only on order edit/list pages
		$current_screen = get_current_screen();
		if ( ! $current_screen ) return; // guard: get_current_screen() can return null
		$screens = array( 'shop_order', 'woocommerce_page_wc-orders', 'post.php' );
		if ( ! in_array( $current_screen->id, $screens ) ) return;

		wp_enqueue_script(
			'parcselect-order-admin',
			PARCSELECT_PLUGIN_URL . 'WOOCOMMERCE_ORDER/assets/order-admin.js',
			array( 'jquery' ),
			PARCSELECT_VERSION,
			true
		);

		wp_localize_script( 'parcselect-order-admin', 'parcselectOrderAdmin', array(
			'nonce' => wp_create_nonce( 'parcselect_order_admin_nonce' ),
		) );
	}
}

Parcselect_Order_Meta_Boxes::init();
}

