<?php
/**
 * Invoice Template
 * Accessed via: /wp-content/plugins/ggm-commerce/templates/invoice.php?order_id=X&key=NONCE
 *
 * @package GGM_Commerce
 */

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter

// Load WordPress if not already loaded (standalone entry point).
if ( ! defined( 'ABSPATH' ) ) {
    $wp_load_path = dirname(__FILE__);
    $max_levels = 10;
    $found = false;

    for ($i = 0; $i < $max_levels; $i++) {
        if (file_exists($wp_load_path . '/wp-load.php')) {
            require_once $wp_load_path . '/wp-load.php';
            $found = true;
            break;
        }
        $parent = dirname($wp_load_path);
        if ($parent === $wp_load_path) {
            break;
        }
        $wp_load_path = $parent;
    }
}

if (!$found) {
    die('WordPress not found');
}

// Security checks
if (!isset($_GET['order_id']) || !isset($_GET['key'])) {
    wp_die(esc_html__('Invalid request', 'ggm-commerce'));
}

$order_id = intval($_GET['order_id']);
$key = sanitize_text_field(wp_unslash($_GET['key']));

// Verify nonce
if (!wp_verify_nonce($key, 'ggmc_print_order_' . $order_id)) {
    wp_die(esc_html__('Security check failed', 'ggm-commerce'));
}

// Check user permissions - must be admin OR the order owner
$can_view = false;
if (current_user_can('manage_options')) {
    $can_view = true;
} else {
    // Check if current user is the order owner (by user_id, customer_id, or email)
    global $wpdb;
    $order_data = $wpdb->get_row($wpdb->prepare(
        "SELECT customer_id, user_id, customer_email FROM {$wpdb->prefix}ggmc_orders WHERE id = %d",
        $order_id
    ));

    if ($order_data) {
        $current_user_id = get_current_user_id();
        $current_user = wp_get_current_user();

        // Check by customer_id
        if (!empty($order_data->customer_id) && $current_user_id === intval($order_data->customer_id)) {
            $can_view = true;
        }
        // Check by user_id
        elseif (!empty($order_data->user_id) && $current_user_id === intval($order_data->user_id)) {
            $can_view = true;
        }
        // Check by email
        elseif (!empty($order_data->customer_email) && $current_user->user_email === $order_data->customer_email) {
            $can_view = true;
        }
    }
}

if (!$can_view) {
    wp_die(esc_html__('Insufficient permissions', 'ggm-commerce'));
}

// Get order data
global $wpdb;
$order = $wpdb->get_row($wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}ggmc_orders WHERE id = %d",
    $order_id
));

if (!$order) {
    wp_die('Order not found');
}

// Get order items
$items = $wpdb->get_results($wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}ggmc_order_items WHERE order_id = %d",
    $order_id
));

// Get settings
$business_options = get_option('ggm_commerce_business_options', []);
$currency_options = get_option('ggm_commerce_currency_options', []);
$currency_symbol = $currency_options['symbol'] ?? '$';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Invoice - Order #<?php echo esc_html($order->order_number); ?></title>
    <style>
        :root {
            --invoice-primary: #1e3a5f;
            --invoice-accent: #2563eb;
            --invoice-success: #059669;
            --invoice-warning: #d97706;
            --invoice-text: #1f2937;
            --invoice-text-muted: #6b7280;
            --invoice-border: #e5e7eb;
            --invoice-bg-subtle: #f9fafb;
        }

        @media print {
            @page {
                margin: 0.5in;
                size: A4;
            }
            body {
                margin: 0;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            .no-print {
                display: none !important;
            }
        }

        * {
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Roboto', 'Helvetica Neue', Arial, sans-serif;
            font-size: 10pt;
            line-height: 1.5;
            color: var(--invoice-text);
            max-width: 8.5in;
            margin: 0 auto;
            padding: 40px;
            background: #f3f4f6;
        }

        .invoice-container {
            background: white;
            padding: 48px;
            box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
            border-radius: 8px;
        }

        @media print {
            body {
                background: white;
                padding: 0;
            }
            .invoice-container {
                box-shadow: none;
                padding: 0;
                border-radius: 0;
            }
            /* Prevent awkward page breaks */
            .header {
                page-break-after: avoid;
                break-after: avoid;
            }
            .addresses {
                page-break-inside: avoid;
                break-inside: avoid;
            }
            .items-table thead {
                display: table-header-group;
            }
            .items-table tbody tr {
                page-break-inside: avoid;
                break-inside: avoid;
            }
            .totals-section {
                page-break-inside: avoid;
                break-inside: avoid;
            }
            .notes-section {
                page-break-inside: avoid;
                break-inside: avoid;
            }
        }

        /* Header */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            padding-bottom: 20px;
            margin-bottom: 15px;
            border-bottom: 2px solid var(--invoice-primary);
        }

        .header-left {
            flex: 1;
        }

        .header-right {
            text-align: right;
            flex: 1;
        }

        .invoice-title {
            font-size: 36pt;
            font-weight: 700;
            color: var(--invoice-primary);
            margin: 0 0 8px 0;
            letter-spacing: -1px;
        }

        .company-info {
            font-size: 9pt;
            color: var(--invoice-text-muted);
            line-height: 1.6;
        }

        .company-info strong {
            display: block;
            font-size: 12pt;
            color: var(--invoice-text);
            margin-bottom: 4px;
        }

        .invoice-meta {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .invoice-meta-row {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            gap: 12px;
        }

        .invoice-meta-label {
            font-size: 8pt;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: var(--invoice-text-muted);
            font-weight: 600;
        }

        .invoice-meta-value {
            font-size: 10pt;
            font-weight: 600;
            color: var(--invoice-text);
            min-width: 120px;
        }

        .invoice-number {
            font-size: 14pt;
            font-weight: 700;
            color: var(--invoice-primary);
        }

        /* Status Badge */
        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 9pt;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-badge.paid {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            color: #065f46;
        }

        .status-badge.pending {
            background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
            color: #92400e;
        }

        .status-badge.failed,
        .status-badge.refunded {
            background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
            color: #991b1b;
        }

        .status-badge::before {
            content: '';
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: currentColor;
        }

        /* Addresses */
        .addresses {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 40px;
            margin-bottom: 15px;
            padding: 10px 24px;
            background: var(--invoice-bg-subtle);
            border-radius: 8px;
        }

        .address-box h3 {
            margin: 0 0 12px 0;
            font-size: 8pt;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: var(--invoice-accent);
            font-weight: 700;
        }

        .address-box p {
            margin: 0;
            font-size: 10pt;
            line-height: 1.7;
            color: var(--invoice-text);
        }

        .address-box strong {
            display: block;
            font-size: 11pt;
            margin-bottom: 4px;
        }

        .address-box .address-email,
        .address-box .address-phone {
            margin-top: 8px;
            color: var(--invoice-text-muted);
            font-size: 9pt;
        }

        /* Items Table */
        .items-section {
            margin-bottom: 32px;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
        }

        .items-table thead th {
            background: var(--invoice-primary);
            color: white;
            padding: 14px 16px;
            text-align: left;
            font-size: 8pt;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            font-weight: 600;
        }

        .items-table thead th:first-child {
            border-radius: 6px 0 0 0;
        }

        .items-table thead th:last-child {
            border-radius: 0 6px 0 0;
        }

        .items-table tbody td {
            padding: 5px 16px;
            border-bottom: 1px solid var(--invoice-border);
            font-size: 10pt;
            vertical-align: top;
        }

        .items-table tbody tr:last-child td {
            border-bottom: 2px solid var(--invoice-primary);
        }

        .items-table tbody tr:nth-child(even) {
            background: var(--invoice-bg-subtle);
        }

        .item-name {
            font-weight: 600;
            color: var(--invoice-text);
        }

        .item-variation {
            display: block;
            font-size: 10pt;
            color: var(--invoice-text-light);
            font-style: italic;
            margin-top: 2px;
        }

        .item-sku {
            display: block;
            font-size: 8pt;
            color: var(--invoice-text-muted);
            margin-top: 2px;
        }

        .text-right,
        .items-table th.text-right,
        .items-table td.text-right {
            text-align: right;
        }

        .text-center,
        .items-table th.text-center,
        .items-table td.text-center {
            text-align: center;
        }

        /* Totals */
        .totals-section {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-bottom: 20px;
        }

        .totals-message {
            flex: 1;
            padding-right: 40px;
        }

        .totals-message .thanks {
            font-size: 14pt;
            font-weight: 600;
            color: var(--invoice-primary);
            margin: 0 0 8px 0;
        }

        .totals-message .vat-number {
            font-size: 9pt;
            color: var(--invoice-text-muted);
            margin: 0;
        }

        .totals-table {
            width: 320px;
            border-collapse: collapse;
        }

        .totals-table td {
            padding: 8px 16px;
            font-size: 10pt;
        }

        .totals-table .totals-label {
            color: var(--invoice-text-muted);
            text-align: left;
        }

        .totals-table .totals-value {
            text-align: right;
            font-weight: 500;
        }

        .totals-table .vat-included {
            font-size: 9pt;
            color: var(--invoice-text-muted);
        }

        .totals-table .total-row {
            background: var(--invoice-primary);
            color: white;
        }

        .totals-table .total-row td {
            padding: 12px 16px;
            font-size: 13pt;
            font-weight: 700;
        }

        .totals-table .total-row td:first-child {
            border-radius: 6px 0 0 6px;
        }

        .totals-table .total-row td:last-child {
            border-radius: 0 6px 6px 0;
        }

        /* Notes */
        .notes-section {
            margin-bottom: 20px;
            padding: 16px 20px;
            background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
            border-left: 4px solid var(--invoice-accent);
            border-radius: 0 8px 8px 0;
        }

        .notes-section h4 {
            margin: 0 0 6px 0;
            font-size: 9pt;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            color: var(--invoice-accent);
            font-weight: 700;
        }

        .notes-section p {
            margin: 0;
            font-size: 10pt;
            color: var(--invoice-text);
            line-height: 1.6;
        }

        /* Print Button */
        .print-button {
            position: fixed;
            top: 24px;
            right: 24px;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 24px;
            background: linear-gradient(135deg, var(--invoice-accent) 0%, #1d4ed8 100%);
            color: white;
            border: none;
            cursor: pointer;
            font-size: 11pt;
            font-weight: 600;
            border-radius: 8px;
            box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
            transition: all 0.2s ease;
        }

        .print-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(37, 99, 235, 0.5);
        }

        .print-button svg {
            width: 18px;
            height: 18px;
        }

        /* Tax Exempt Notice */
        .tax-exempt-notice {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            font-size: 8pt;
            color: var(--invoice-text-muted);
            font-style: italic;
        }
    </style>
</head>
<body>
    <button onclick="window.print()" class="print-button no-print">
        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
        </svg>
        Print Invoice
    </button>

    <div class="invoice-container">
        <div class="header">
            <div class="header-left">
                <h1 class="invoice-title">INVOICE</h1>
                <div class="company-info">
                    <?php if (!empty($business_options['name'])): ?>
                        <strong><?php echo esc_html($business_options['name']); ?></strong>
                    <?php endif; ?>
                    <?php if (!empty($business_options['address'])): ?>
                        <?php echo nl2br(esc_html($business_options['address'])); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($business_options['phone'])): ?>
                        <?php echo esc_html($business_options['phone']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($business_options['email'])): ?>
                        <?php echo esc_html($business_options['email']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($business_options['website'])): ?>
                        <?php echo esc_html($business_options['website']); ?>
                    <?php endif; ?>
                </div>
            </div>
            <div class="header-right">
                <div class="invoice-meta">
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Invoice No.</span>
                        <span class="invoice-meta-value invoice-number"><?php echo esc_html($order->order_number); ?></span>
                    </div>
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Date</span>
                        <span class="invoice-meta-value"><?php echo esc_html(gmdate('F j, Y', strtotime($order->created_at))); ?></span>
                    </div>
                    <?php if ($order->payment_method): ?>
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Payment</span>
                        <span class="invoice-meta-value"><?php echo esc_html(ucfirst(str_replace('_', ' ', $order->payment_method))); ?></span>
                    </div>
                    <?php endif; ?>
                    <div class="invoice-meta-row">
                        <span class="invoice-meta-label">Status</span>
                        <span class="status-badge <?php echo esc_attr($order->payment_status); ?>"><?php echo esc_html(ucfirst($order->payment_status)); ?></span>
                    </div>
                </div>
            </div>
        </div>

        <div class="addresses">
            <div class="address-box">
                <h3>Bill To</h3>
                <p>
                    <strong><?php echo esc_html($order->customer_first_name . ' ' . $order->customer_last_name); ?></strong>
                    <?php if ($order->billing_address_1): ?>
                        <?php echo esc_html($order->billing_address_1); ?><br>
                        <?php if ($order->billing_address_2): ?>
                            <?php echo esc_html($order->billing_address_2); ?><br>
                        <?php endif; ?>
                        <?php echo esc_html($order->billing_city); ?><?php if ($order->billing_state): ?>, <?php echo esc_html($order->billing_state); ?><?php endif; ?> <?php echo esc_html($order->billing_postcode); ?><br>
                        <?php echo esc_html($order->billing_country); ?>
                    <?php endif; ?>
                    <span class="address-email"><?php echo esc_html($order->customer_email); ?></span>
                    <?php if ($order->customer_phone): ?>
                        <span class="address-phone"><?php echo esc_html($order->customer_phone); ?></span>
                    <?php endif; ?>
                </p>
            </div>
            <div class="address-box">
                <h3>Ship To</h3>
                <p>
                    <strong><?php echo esc_html($order->customer_first_name . ' ' . $order->customer_last_name); ?></strong>
                    <?php if ($order->shipping_address_1): ?>
                        <?php echo esc_html($order->shipping_address_1); ?><br>
                        <?php if ($order->shipping_address_2): ?>
                            <?php echo esc_html($order->shipping_address_2); ?><br>
                        <?php endif; ?>
                        <?php echo esc_html($order->shipping_city); ?><?php if ($order->shipping_state): ?>, <?php echo esc_html($order->shipping_state); ?><?php endif; ?> <?php echo esc_html($order->shipping_postcode); ?><br>
                        <?php echo esc_html($order->shipping_country); ?>
                    <?php else: ?>
                        <em style="color: var(--invoice-text-muted);">Same as billing address</em>
                    <?php endif; ?>
                </p>
            </div>
        </div>

        <?php
        // Determine VAT/Tax display logic
        $prices_include_tax = !empty($order->prices_include_tax);
        $tax_amount = (float)($order->tax_amount ?? 0);
        $tax_rate = !empty($order->tax_rate) ? (float)$order->tax_rate : 0;
        $tax_label = $order->tax_label ?? 'VAT';
        $is_tax_exempt = !empty($order->is_tax_exempt) && $order->is_tax_exempt;
        $vat_number = $business_options['tax_id'] ?? '';
        $is_vat_registered = !empty($vat_number);

        // Calculate per-item tax if we have items and a tax rate
        // For ex-VAT prices: item_tax = item_price * (tax_rate / 100)
        // For inc-VAT prices: item already includes tax
        ?>
        <div class="items-section">
            <table class="items-table">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th style="width: 60px;" class="text-center">Qty</th>
                        <th style="width: 100px;" class="text-right">Unit Price</th>
                        <?php if ($is_vat_registered): ?>
                        <th style="width: 80px;" class="text-right"><?php echo esc_html($tax_label); ?></th>
                        <?php endif; ?>
                        <th style="width: 100px;" class="text-right">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item):
                        $item_price = (float)$item->price;
                        $item_qty = (int)$item->quantity;
                        $item_line_total = (float)$item->total;

                        if ($prices_include_tax) {
                            // Prices include VAT - show "incl." in VAT column
                            $item_vat_display = 'incl.';
                        } elseif ($is_tax_exempt) {
                            // B2B exempt - show £0.00
                            $item_vat_display = $currency_symbol . '0.00';
                        } elseif ($tax_rate > 0) {
                            // Calculate VAT per line item
                            $item_vat = $item_line_total * ($tax_rate / 100);
                            $item_vat_display = $currency_symbol . number_format($item_vat, 2);
                        } else {
                            $item_vat_display = $currency_symbol . '0.00';
                        }

                        // Calculate line total including VAT (for ex-VAT display)
                        if (!$prices_include_tax && !$is_tax_exempt && $tax_rate > 0) {
                            $item_total_with_vat = $item_line_total * (1 + ($tax_rate / 100));
                        } else {
                            $item_total_with_vat = $item_line_total;
                        }

                        // Get variation display - use stored data or look up from variation_id
                        $variation_display = '';
                        if (!empty($item->variation_data)) {
                            $variation_display = $item->variation_data;
                        } elseif (!empty($item->variation_id) && class_exists('GGM_Commerce_Variations')) {
                            // Fallback: look up variation attributes from variation_id
                            $variations_manager = GGM_Commerce_Variations::instance();
                            $variation = $variations_manager->get_variation($item->variation_id);
                            if ($variation && !empty($variation['attributes'])) {
                                $attr_parts = [];
                                foreach ($variation['attributes'] as $attr) {
                                    $attr_name = $attr['attribute_name'] ?? $attr['name'] ?? '';
                                    $attr_value = $attr['attribute_value'] ?? $attr['value'] ?? '';
                                    if (!empty($attr_name) && !empty($attr_value)) {
                                        $attr_parts[] = $attr_name . ': ' . $attr_value;
                                    }
                                }
                                if (!empty($attr_parts)) {
                                    $variation_display = implode(', ', $attr_parts);
                                }
                            }
                        }
                    ?>
                        <tr>
                            <td>
                                <span class="item-name"><?php echo esc_html($item->product_name); ?></span>
                                <?php if (!empty($variation_display)): ?>
                                    <span class="item-variation"><?php echo esc_html($variation_display); ?></span>
                                <?php endif; ?>
                                <?php if (!empty($item->product_sku)): ?>
                                    <span class="item-sku">SKU: <?php echo esc_html($item->product_sku); ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center"><?php echo esc_html($item_qty); ?></td>
                            <td class="text-right"><?php echo esc_html($currency_symbol . number_format($item_price, 2)); ?></td>
                            <?php if ($is_vat_registered): ?>
                            <td class="text-right"><?php echo esc_html($item_vat_display); ?></td>
                            <?php endif; ?>
                            <td class="text-right"><?php echo esc_html($currency_symbol . number_format($item_total_with_vat, 2)); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="totals-section">
            <div class="totals-message">
                <p class="thanks">Thank you for your business!</p>
                <?php if ($is_vat_registered): ?>
                    <p class="vat-number"><?php echo esc_html($tax_label); ?> No: <?php echo esc_html($vat_number); ?></p>
                <?php endif; ?>
            </div>
            <table class="totals-table">
                <?php if ($prices_include_tax): ?>
                    <!-- Prices include VAT -->
                    <tr>
                        <td class="totals-label">Subtotal</td>
                        <td class="totals-value"><?php echo esc_html($currency_symbol . number_format($order->subtotal, 2)); ?></td>
                    </tr>
                    <?php if ($tax_amount > 0 && !$is_tax_exempt): ?>
                    <tr>
                        <td class="totals-label"><?php echo esc_html($tax_label); ?><?php if ($tax_rate > 0): ?> @ <?php echo esc_html($tax_rate); ?>%<?php endif; ?></td>
                        <td class="totals-value vat-included">(<?php echo esc_html($currency_symbol . number_format($tax_amount, 2)); ?> incl.)</td>
                    </tr>
                    <?php endif; ?>
                <?php else: ?>
                    <!-- Prices exclude VAT -->
                    <tr>
                        <td class="totals-label">Net Total</td>
                        <td class="totals-value"><?php echo esc_html($currency_symbol . number_format($order->subtotal, 2)); ?></td>
                    </tr>
                    <?php if ($is_tax_exempt): ?>
                    <tr>
                        <td class="totals-label">
                            <?php echo esc_html($tax_label); ?>
                            <span class="tax-exempt-notice">(Exempt)</span>
                        </td>
                        <td class="totals-value"><?php echo esc_html($currency_symbol . '0.00'); ?></td>
                    </tr>
                    <?php elseif ($tax_amount > 0): ?>
                    <tr>
                        <td class="totals-label"><?php echo esc_html($tax_label); ?><?php if ($tax_rate > 0): ?> @ <?php echo esc_html($tax_rate); ?>%<?php endif; ?></td>
                        <td class="totals-value"><?php echo esc_html($currency_symbol . number_format($tax_amount, 2)); ?></td>
                    </tr>
                    <?php endif; ?>
                <?php endif; ?>
                <?php if ((float)$order->shipping_amount > 0 || !empty($order->shipping_method_name)): ?>
                <tr>
                    <td class="totals-label"><?php echo !empty($order->shipping_method_name) ? esc_html($order->shipping_method_name) : 'Shipping'; ?></td>
                    <td class="totals-value">
                        <?php
                        if ((float)$order->shipping_amount > 0) {
                            echo esc_html($currency_symbol . number_format($order->shipping_amount, 2));
                        } else {
                            esc_html_e('Free', 'ggm-commerce');
                        }
                        ?>
                    </td>
                </tr>
                <?php endif; ?>
                <?php if (!empty($order->discount_amount) && (float)$order->discount_amount > 0): ?>
                <tr>
                    <td class="totals-label">Discount</td>
                    <td class="totals-value">-<?php echo esc_html($currency_symbol . number_format($order->discount_amount, 2)); ?></td>
                </tr>
                <?php endif; ?>
                <tr class="total-row">
                    <td>Total<?php if ($prices_include_tax && $tax_amount > 0 && !$is_tax_exempt): ?> <span style="font-size: 9pt; font-weight: 400;">(inc. <?php echo esc_html($tax_label); ?>)</span><?php endif; ?></td>
                    <td class="text-right"><?php echo esc_html($currency_symbol . number_format($order->total_amount, 2)); ?></td>
                </tr>
            </table>
        </div>

        <?php
        // Get customer-visible notes only
        $customer_notes = GGM_Commerce_Order_Notes::get_customer_notes($order_id);
        if (!empty($customer_notes)):
        ?>
            <div class="notes-section">
                <h4>Order Notes</h4>
                <?php foreach ($customer_notes as $note): ?>
                    <p><?php echo nl2br(esc_html($note->content)); ?></p>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>
