<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/helpers/auth.php';
require_once __DIR__ . '/../app/helpers/settings.php';

require_admin();

$pageTitle = 'Email Logs';

$logs = $pdo->query("
    SELECT el.*,
           c.first_name,
           c.last_name,
           c.email AS customer_email,
           o.order_number
    FROM email_logs el
    LEFT JOIN customers c ON el.customer_id = c.id
    LEFT JOIN orders o ON el.order_id = o.id
    ORDER BY el.id DESC
")->fetchAll();

require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/sidebar.php';
?>

<main class="admin-main">
    <div class="admin-topbar">
        <div>
            <h1>Email Logs</h1>
            <div class="admin-user">View queued, sent, and failed email records.</div>
        </div>
    </div>

    <section class="admin-panel">
        <h2>All Email Logs</h2>

        <table class="admin-table">
            <thead>
                <tr>
                    <th>Email To</th>
                    <th>Subject</th>
                    <th>Customer</th>
                    <th>Order</th>
                    <th>Status</th>
                    <th>Error</th>
                    <th>Sent At</th>
                    <th>Created</th>
                </tr>
            </thead>

            <tbody>
                <?php if (!$logs): ?>
                    <tr>
                        <td colspan="8">No email logs yet.</td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($logs as $log): ?>
                    <tr>
                        <td><?= e($log['email_to']); ?></td>

                        <td><strong><?= e($log['subject']); ?></strong></td>

                        <td>
                            <?php if (!empty($log['customer_id'])): ?>
                                <?= e(trim(($log['first_name'] ?? '') . ' ' . ($log['last_name'] ?? '')) ?: 'Customer'); ?><br>
                                <small><?= e($log['customer_email'] ?? '-'); ?></small>
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>

                        <td><?= e($log['order_number'] ?? '-'); ?></td>

                        <td><?= e(ucfirst($log['status'])); ?></td>

                        <td><?= e($log['error_message'] ?? '-'); ?></td>

                        <td>
                            <?= !empty($log['sent_at'])
                                ? e(date('d M Y, h:i A', strtotime($log['sent_at'])))
                                : '-'; ?>
                        </td>

                        <td><?= e(date('d M Y, h:i A', strtotime($log['created_at']))); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </section>
</main>

<?php require_once __DIR__ . '/includes/footer.php'; ?>