<?php
/**
 * Orders List Template
 */

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

if (!is_user_logged_in()) {
    $accountUrl = \Instinctor\Services\PageService::resolveUrl('account');
    echo '<p>' . esc_html__('Please log in to view your orders.', 'instinctor') . '</p>';
    echo '<a href="' . esc_url($accountUrl) . '" class="instinctor-btn instinctor-btn-primary">' . esc_html__('Log In', 'instinctor') . '</a>';
    return;
}

$user = wp_get_current_user();

global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.SlowDBQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter -- direct query against the plugin's own custom table; the WP options/post API does not model this data and the read is request-scoped (object caching would add complexity without benefit). The interpolated Tenant::scopeSql() fragment carries no request data: the column is a caller-supplied literal and the owner id is int-cast inside scopeSqlFor(); the user values are bound through prepare() placeholders
$customer = $wpdb->get_row($wpdb->prepare(
    // Email unique per owner (0058): keep the email fallback inside the current
    // scope or it could match another tenant's client row.
    "SELECT * FROM {$wpdb->prefix}instinctor_customers
     WHERE (user_id = %d OR email = %s) AND " . \Instinctor\Core\Tenant::scopeSql('owner_id'),
    $user->ID,
    $user->user_email
));

$orders = [];
if ($customer) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.SlowDBQuery -- direct query against the plugin's own custom table; the WP options/post API does not model this data and the read is request-scoped (object caching would add complexity without benefit)
    $orders = $wpdb->get_results($wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}instinctor_orders WHERE customer_id = %d ORDER BY created_at DESC",
        $customer->id
    ));
}

$statuses = [
    'pending' => __('Pending', 'instinctor'),
    'processing' => __('Processing', 'instinctor'),
    'completed' => __('Completed', 'instinctor'),
    'cancelled' => __('Cancelled', 'instinctor'),
    'refunded' => __('Refunded', 'instinctor'),
];
?>

<div class="instinctor-orders">
    <h1><?php esc_html_e('My Orders', 'instinctor'); ?></h1>
    
    <?php if ($orders): ?>
        <table class="instinctor-table" style="width: 100%; border-collapse: collapse;">
            <thead>
                <tr>
                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid #eee;"><?php esc_html_e('Order', 'instinctor'); ?></th>
                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid #eee;"><?php esc_html_e('Date', 'instinctor'); ?></th>
                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid #eee;"><?php esc_html_e('Status', 'instinctor'); ?></th>
                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid #eee;"><?php esc_html_e('Total', 'instinctor'); ?></th>
                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid #eee;"><?php esc_html_e('Actions', 'instinctor'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orders as $acOrder): ?>
                    <tr>
                        <td style="padding: 12px; border-bottom: 1px solid #eee;">
                            <strong>#<?php echo esc_html($acOrder->order_number); ?></strong>
                        </td>
                        <td style="padding: 12px; border-bottom: 1px solid #eee;">
                            <?php echo esc_html(date_i18n(get_option('date_format'), strtotime($acOrder->created_at))); ?>
                        </td>
                        <td style="padding: 12px; border-bottom: 1px solid #eee;">
                            <span class="instinctor-status instinctor-status-<?php echo esc_attr($acOrder->status); ?>">
                                <?php echo esc_html($statuses[$acOrder->status] ?? ucfirst($acOrder->status)); ?>
                            </span>
                        </td>
                        <td style="padding: 12px; border-bottom: 1px solid #eee;">
                            <?php echo esc_html(\Instinctor\Services\CurrencyService::formatPrice($acOrder->total)); ?>
                        </td>
                        <td style="padding: 12px; border-bottom: 1px solid #eee;">
                            <a href="<?php echo esc_url(add_query_arg('view-order', $acOrder->id, \Instinctor\Services\PageService::resolveUrl('orders'))); ?>" class="instinctor-btn instinctor-btn-secondary" style="padding: 6px 12px; font-size: 13px;">
                                <?php esc_html_e('View', 'instinctor'); ?>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php else: ?>
        <div class="instinctor-empty-state" style="text-align: center; padding: 60px 20px;">
            <div style="font-size: 48px; margin-bottom: 16px;">📦</div>
            <h3><?php esc_html_e('No orders yet', 'instinctor'); ?></h3>
            <p style="color: #666;"><?php esc_html_e('When you place orders, they will appear here.', 'instinctor'); ?></p>
            <a href="<?php echo esc_url(\Instinctor\Services\PageService::resolveUrl('shop')); ?>" class="instinctor-btn instinctor-btn-primary" style="margin-top: 16px;">
                <?php esc_html_e('Start Shopping', 'instinctor'); ?>
            </a>
        </div>
    <?php endif; ?>
</div>
