<?php
/**
 * Frontend View for WooCommerce Orders (Thank You & My Account).
 * 
 * @package ParcelSelector
 */

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

// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment -- Translators comments are placed before sprintf() wrapper, not before each individual __()/_e() call

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

	public static function init() {
		// 1. Thank-you page / Order received
		add_action( 'woocommerce_thankyou', array( __CLASS__, 'display_order_details' ), 10, 1 );
		
		// 2. My Account order detail view
		add_action( 'woocommerce_order_details_after_order_table', array( __CLASS__, 'display_order_details' ), 10, 1 );
	}

	/**
	 * Display locker info.
	 * 
	 * @param int|WC_Order $order_id_or_object
	 */
	public static function display_order_details( $order_id_or_object ) {
		$order = ( $order_id_or_object instanceof WC_Order ) ? $order_id_or_object : wc_get_order( $order_id_or_object );
		
		if ( ! $order ) return;

		$data = parcselect_get_order_data( $order );
		if ( ! $data['has_data'] ) return;

		// Skip duplicate display: woocommerce_thankyou fires on order-received page,
		// woocommerce_order_details_after_order_table fires on My Account orders.
		// is_order_received_page() correctly identifies the thank-you page.
		if ( is_order_received_page() && doing_action( 'woocommerce_order_details_after_order_table' ) ) return;

		if ( function_exists( 'parcselect_render_locker_card' ) ) {
			// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
			echo parcselect_render_locker_card( $data['paczkomat'], 'frontend', $order );
			// phpcs:enable
		}

		// Link to local SEO page — drives internal traffic + builds page authority.
		if ( 'yes' === get_option( 'parcselect_local_seo_enabled', 'no' ) && class_exists( 'Parcselect_Local_SEO' ) ) {
			$city = ! empty( $data['paczkomat']['city'] ) ? $data['paczkomat']['city'] : '';
			if ( $city ) {
				$base_slug = Parcselect_Local_SEO::get_base_slug();
				$city_slug = sanitize_title( $city );
				$city_url  = home_url( $base_slug . '/' . $city_slug . '/' );
				echo '<div style="margin-top:8px;font-size:13px;color:#666">';
				$_ps_nearby = sprintf(
					/* translators: %s: link to city pickup points page */
					esc_html__( 'Browse all pickup points in %s', 'parcel-selector' ),
					'<a href="' . esc_url( $city_url ) . '" style="color:#f97316">' . esc_html( $city ) . '</a>'
				);
				echo wp_kses_post( $_ps_nearby ) . ' →';
				echo '</div>';
			}
		}
	}
}

Parcselect_Order_Frontend_View::init();
}

