<?php

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

class R123_WC_My_Account {
	public static function init(): void {
		add_filter('woocommerce_my_account_my_orders_actions', [self::class, 'add_pdf_action_to_orders_list'], 10, 2);
		add_action('woocommerce_order_details_after_order_table', [self::class, 'render_order_invoice_block'], 20);
	}

	public static function add_pdf_action_to_orders_list(array $actions, WC_Order $order): array {
		if (!self::can_access_invoice_from_orders_list($order)) {
			return $actions;
		}

		$pdf_url = (string) $order->get_meta('_r123_pdf_url');

		if ($pdf_url === '') {
			return $actions;
		}

		$actions['r123_pdf_invoice'] = [
			'url'  => esc_url($pdf_url),
			'name' => __('PDF račun', 'racun123-for-woocommerce'),
		];

		return $actions;
	}

	public static function render_order_invoice_block($order): void {
		if (!($order instanceof WC_Order)) {
			return;
		}

		if (!self::can_access_invoice_for_order_details($order)) {
			return;
		}

		$invoice_number = (string) $order->get_meta('_r123_invoice_number');
		$pdf_url        = (string) $order->get_meta('_r123_pdf_url');

		if ($invoice_number === '' && $pdf_url === '') {
			return;
		}

		echo '<section class="woocommerce-order-details r123-order-invoice" style="margin-top:24px;">';
		echo '<h2>' . esc_html__('Račun', 'racun123-for-woocommerce') . '</h2>';

		if ($invoice_number !== '') {
			echo '<p><strong>' . esc_html__('Št. računa:', 'racun123-for-woocommerce') . '</strong> ' . esc_html($invoice_number) . '</p>';
		}

		if ($pdf_url !== '') {
			echo '<p>';
			echo '<a class="button" href="' . esc_url($pdf_url) . '" target="_blank" rel="noopener noreferrer">';
			echo esc_html__('Prenesi PDF račun', 'racun123-for-woocommerce');
			echo '</a>';
			echo '</p>';
		}

		echo '</section>';
	}

	private static function can_access_invoice_from_orders_list(WC_Order $order): bool {
		$current_user_id = get_current_user_id();

		if (!$current_user_id) {
			return false;
		}

		$order_user_id = (int) $order->get_user_id();

		if ($order_user_id <= 0) {
			return false;
		}

		return $current_user_id === $order_user_id;
	}

	private static function can_access_invoice_for_order_details(WC_Order $order): bool {
		$current_user_id = get_current_user_id();
		$order_user_id   = (int) $order->get_user_id();

		// Registered customer viewing their own order
		if ($current_user_id && $order_user_id > 0 && $current_user_id === $order_user_id) {
			return true;
		}

		// Guest order access via valid order key in URL
		if ($order_user_id <= 0 && self::has_valid_guest_order_access($order)) {
			return true;
		}

		return false;
	}

	private static function has_valid_guest_order_access(WC_Order $order): bool {
		$order_key = isset($_GET['key']) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only order key validation.
			? sanitize_text_field(wp_unslash($_GET['key'])) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only order key validation.
			: '';

		if ($order_key === '') {
			return false;
		}

		return hash_equals((string) $order->get_order_key(), $order_key);
	}
}