<?php
/**
 * Email View for WooCommerce Orders.
 * 
 * @package ParcelSelector
 */

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

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

	public static function init() {
		add_action( 'woocommerce_email_after_order_table', array( __CLASS__, 'display_email_order_details' ), 10, 4 );
	}

	/**
	 * Render locker info in emails.
	 * 
	 * @param WC_Order $order
	 * @param bool     $sent_to_admin
	 * @param bool     $plain_text
	 * @param WC_Email $email
	 */
	public static function display_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
		if ( ! is_a( $order, 'WC_Order' ) ) return;

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

		$p = $data['paczkomat'];

		if ( $plain_text ) {
			$section = empty( $p['is_custom'] )
				? esc_html__( 'InPost Parcel Locker', 'parcel-selector' )
				: esc_html__( 'Punkt odbioru osobistego', 'parcel-selector' );
			
			echo "\n--- " . $section . " ---\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			
			$key_label = empty( $p['is_custom'] )
				? esc_html__( 'Locker:', 'parcel-selector' )
				: esc_html__( 'Punkt:', 'parcel-selector' );
			
			echo $key_label . ' ' . esc_html( $p['name'] ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			
			if ( $p['address1'] ) {
				echo esc_html__( 'Address:', 'parcel-selector' ) . ' ' . esc_html( $p['address1'] ) . "\n";
			}
			echo esc_html__( 'City:', 'parcel-selector' ) . ' ' . esc_html( $p['city'] ) . "\n";
			if ( $p['country_name'] ) {
				echo esc_html__( 'Country:', 'parcel-selector' ) . ' ' . esc_html( $p['country_name'] ) . "\n";
			}
			echo "\n";
		} else {
			if ( function_exists( 'parcselect_render_locker_card' ) ) {
				// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
				echo parcselect_render_locker_card( $p, 'email', $order );
				// phpcs:enable
			}
		}
	}
}

Parcselect_Order_Email_View::init();
}

