<?php
/**
 * Shared rendering utilities for WooCommerce Order displays.
 * 
 * @package ParcelSelector
 */

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

/**
 * Render the locker info card HTML.
 */
if ( ! function_exists( 'parcselect_render_locker_card' ) ) {
function parcselect_render_locker_card( $paczkomat, $context = 'frontend', $order_input = null ) {
	$order = null;
	if ( $order_input ) {
		$order = ( $order_input instanceof WC_Order ) ? $order_input : wc_get_order( $order_input );
	}
	// Use order->get_id() when available; get_the_ID() only works reliably inside The Loop
	$order_id = $order ? $order->get_id() : (int) get_the_ID();
	if ( ! $order_id ) {
		$order_id = 0;
	}
	$name         = esc_html( $paczkomat['name'] );
	$address      = esc_html( $paczkomat['address1'] );
	$city         = esc_html( $paczkomat['city'] );
	$post_code    = esc_html( $paczkomat['post_code'] );
	$country_name = esc_html( $paczkomat['country_name'] );
	$flag         = parcselect_get_country_flag( $paczkomat['country'] );

	$city_line = $post_code ? "({$post_code}) {$city}" : $city;

	if ( 'admin' === $context ) {
		$bg    = '#f0f9ff';
		$border = '#0073aa';
		$title_color = '#0073aa';
	} elseif ( 'email' === $context ) {
		$bg    = '#fff7ed';
		$border = '#fed7aa';
		$title_color = '#ea580c';
	} else {
		$bg    = '#fff7ed';
		$border = '#fed7aa';
		$title_color = '#ea580c';
	}

	$is_custom = ! empty( $paczkomat['is_custom'] );
	$icon      = $is_custom ? '&#x1F3E0;' : '&#x1F4E6;';

	$t         = function_exists( 'parcselect_admin_i18n' ) ? parcselect_admin_i18n() : array();
	$title     = $is_custom
		? esc_html( isset( $t['order_pickup_title'] ) ? $t['order_pickup_title'] : __( 'Pickup point', 'parcel-selector' ) )
		: esc_html( isset( $t['order_locker_title'] ) ? $t['order_locker_title'] : __( 'Parcel Locker', 'parcel-selector' ) );

	if ( $is_custom && 'admin' === $context ) {
		$border      = '#16a34a';
		$title_color = '#166534';
		$bg          = '#f0fdf4';
	}

	$html  = '<div style="background:' . $bg . ';border:2px solid ' . $border . ';border-radius:10px;padding:12px 14px;margin:8px 0;">';
	$html .= '<p style="margin:0 0 8px;font-weight:700;color:' . $title_color . ';font-size:13px;">' . $icon . ' ' . $title . '</p>';

	// ─── Locker ID (most important) ───────────────────────────────────────
	$html .= '<p style="margin:0 0 2px;font-weight:700;font-size:16px;letter-spacing:0.5px;color:#0f172a;">' . $name . '</p>';

	// ─── Address ─────────────────────────────────────────────────────────
	if ( $address ) {
		$html .= '<p style="margin:2px 0;font-size:12px;color:#555;">' . $address . '</p>';
	}
	$html .= '<p style="margin:2px 0;font-size:12px;color:#555;">' . esc_html( $city_line ) . '</p>';

	// ─── Country ─────────────────────────────────────────────────────────
	if ( $country_name ) {
		$html .= '<p style="margin:6px 0 0;font-size:12px;color:#888;font-weight:600;">' . $flag . ' ' . $country_name . '</p>';
	}

	// ─── Admin stats: selection time ───────────────
	if ( 'admin' === $context && $order ) {
		$_ps_select = $order->get_meta( '_ps_time_to_order', true );
		$_ps_device = $order->get_meta( '_ps_device_type', true );

		if ( $_ps_select ) {
			$html .= '<div style="margin:8px 0 0;padding:8px 0 0;border-top:1px solid rgba(0,0,0,0.06);font-size:11px;color:#888;">';
			$html .= '<p style="margin:0 0 3px;">&#x23F1; ' . esc_html( $_ps_select );
			if ( $_ps_device ) {
				$html .= ' <span style="color:#aaa;">(' . esc_html( $_ps_device ) . ')</span>';
			}
			$html .= '</p>';
			$html .= '</div>';
		}
	}

	// ─── Order note (if present) ──────────────────────────────────────────
	if ( ! empty( $paczkomat['order_note'] ) ) {
		$html .= '<p style="margin:6px 0 0;font-size:11px;color:#888;font-style:italic;border-top:1px solid rgba(0,0,0,0.06);padding-top:6px;">'
			. esc_html( $paczkomat['order_note'] ) . '</p>';
	}

	$html .= '</div>';

	return $html;
}
}

/**
 * Global helper to render NASZA WYŚWIETLAJKA.
 */
if ( ! function_exists( 'parcselect_render_server_notice' ) ) {
function parcselect_render_server_notice( $context = 'panel' ) {
	$notice = get_option( 'parcselect_server_notice', null );
	if ( empty( $notice ) || empty( $notice['text'] ) ) return;

	if ( ! empty( $notice['expires_at'] ) && strtotime( $notice['expires_at'] ) < time() ) {
		delete_option( 'parcselect_server_notice' );
		return;
	}

	// Free version: no license system, render notices unfiltered.

	$type_map = array(
		'info'    => array( 'bg' => '#EFF6FF', 'border' => '#3B82F6', 'icon' => '&#x2139;&#xFE0F;', 'wp_class' => 'notice-info' ),
		'warning' => array( 'bg' => '#FFFBEB', 'border' => '#F59E0B', 'icon' => '&#x26A0;&#xFE0F;', 'wp_class' => 'notice-warning' ),
		'error'   => array( 'bg' => '#FEF2F2', 'border' => '#EF4444', 'icon' => '&#x274C;',         'wp_class' => 'notice-error' ),
		'success' => array( 'bg' => '#F0FDF4', 'border' => '#10B981', 'icon' => '&#x2705;',         'wp_class' => 'notice-success' ),
	);
	$t    = $type_map[ $notice['type'] ] ?? $type_map['info'];
	$text = esc_html( $notice['text'] );
	$link = '';
	if ( ! empty( $notice['link_url'] ) && ! empty( $notice['link_text'] ) ) {
		$link = ' <a href="' . esc_url( $notice['link_url'] ) . '" target="_blank" rel="noopener" style="color:inherit; font-weight:600;">'
			. esc_html( $notice['link_text'] ) . ' &rarr;</a>';
	}

	$html_block = ! empty( $notice['html_content'] ) ? '<div class="ps-notice-html" style="margin-top:10px; font-size:13px; line-height:1.6;">' . wp_kses( $notice['html_content'], parcselect_allowed_notice_html() ) . '</div>' : '';
	$code_block = ! empty( $notice['code_snippet'] ) ? '<div class="ps-notice-code" style="margin-top:10px;"><code style="display:block; background:#1e1e1e; color:#d4d4d4; font-family:monospace; font-size:13px; padding:10px 14px; border-radius:4px; word-break:break-all; cursor:pointer; user-select:all;">' . esc_html( $notice['code_snippet'] ) . '</code></div>' : '';

	if ( $context === 'admin_notice' ) {
		echo '<div class="notice ' . esc_attr( $t['wp_class'] ) . ' is-dismissible ps-server-notice"><p>' . $t['icon'] . ' <strong>ParcelSelector:</strong> ' . $text . $link . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $t['icon'] is static HTML entity, $text/$link are pre-escaped.
	} elseif ( $context === 'wc_order' ) {
		echo '<div class="ps-server-notice" style="background:' . esc_attr( $t['bg'] ) . '; border:2px solid ' . esc_attr( $t['border'] ) . '; padding:12px 14px; margin:10px 0; border-radius:6px; font-size:13px;">'
			. '<div><strong>' . $t['icon'] . ' ParcelSelector.com:</strong> ' . $text . $link . '</div>' . $html_block . $code_block . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $t['icon'] is static HTML entity, $text/$link are pre-escaped, $html_block/$code_block are internally generated HTML blocks.
	} else {
		echo '<div class="ps-server-notice" style="background:' . esc_attr( $t['bg'] ) . '; border-left:4px solid ' . esc_attr( $t['border'] ) . '; padding:14px 18px; margin-bottom:20px; border-radius:4px; font-size:14px;">'
			. '<div style="display:flex; align-items:flex-start; gap:10px;"><span style="font-size:20px; flex-shrink:0;">' . $t['icon'] . '</span>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $t['icon'] is a static emoji from a controlled internal array.
			. '<div style="flex:1;"><strong>ParcelSelector.com:</strong> ' . $text . $link . $html_block . $code_block . '</div></div></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $text/$link are pre-escaped, $html_block/$code_block are internally generated HTML blocks.
	}
}
}

/**
 * Allowed HTML tags for server_notice.
 */
if ( ! function_exists( 'parcselect_allowed_notice_html' ) ) {
function parcselect_allowed_notice_html() {
	// Hardened 2026-05-17: no style attribute anywhere. Server-supplied notice
	// HTML must not be able to inject CSS overlays into wp-admin.
	return array(
		'strong' => array(), 'em' => array(), 'b' => array(), 'i' => array(), 'br' => array(),
		'p' => array(), 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
		'span' => array(), 'div' => array(),
		'img' => array( 'src' => array(), 'alt' => array(), 'width' => array(), 'height' => array() ),
		'ul' => array(), 'ol' => array(), 'li' => array(), 'hr' => array(), 'small' => array(),
	);
}
}
