<?php
/**
 * Rig N Dig - Admin Mailbox
 * Manage incoming contact floor inquiries and RFQs
 */

$adminTitle = 'Mailbox';
require_once __DIR__ . '/includes/header.php';

// Handle Actions
if (isset($_GET['action'])) {
    $msgId = (int)($_GET['id'] ?? 0);
    if ($_GET['action'] === 'read') {
        Database::execute("UPDATE messages SET is_read = 1 WHERE id = ?", [$msgId]);
    } elseif ($_GET['action'] === 'unread') {
        Database::execute("UPDATE messages SET is_read = 0 WHERE id = ?", [$msgId]);
    } elseif ($_GET['action'] === 'delete') {
        Database::execute("DELETE FROM messages WHERE id = ?", [$msgId]);
        $_SESSION['flash']['success'] = "Message permanently deleted.";
        header("Location: mailbox.php");
        exit;
    }
}

// View Message Detail
$viewId = (int)($_GET['view'] ?? 0);
if ($viewId) {
    Database::execute("UPDATE messages SET is_read = 1 WHERE id = ?", [$viewId]);
    $msg = Database::fetchOne("SELECT * FROM messages WHERE id = ?", [$viewId]);
    if ($msg):
?>
    <div class="mb-6 flex items-center gap-4">
        <a href="mailbox.php" class="text-gray-400 hover:text-white">← Back to Inbox</a>
        <div class="h-4 w-px bg-navy-500"></div>
        <div class="text-xs text-gray-500">Received on <?= date('M d, Y @ H:i', strtotime($msg['created_at'])) ?></div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <div class="lg:col-span-2">
            <div class="card-dark p-8">
                <div class="mb-8">
                    <div class="text-xs font-bold text-orange-500 uppercase tracking-widest mb-2"><?= e($msg['subject'] ?: 'No Subject') ?></div>
                    <h2 class="text-xl font-bold text-white"><?= e($msg['name']) ?> wrote:</h2>
                </div>
                <div class="prose prose-invert max-w-none text-gray-300 leading-relaxed whitespace-pre-line text-sm">
                    <?= e($msg['message']) ?>
                </div>
                <div class="mt-12 pt-8 border-t border-navy-500 flex gap-4">
                    <a href="mailto:<?= e($msg['email']) ?>" class="btn-primary text-xs px-6 py-2.5">📧 Reply via Email</a>
                    <a href="?action=delete&id=<?= $msg['id'] ?>" class="btn-outline border-red-900/50 text-red-500 hover:bg-red-900/20 text-xs px-6 py-2.5" onclick="return confirm('Delete this message permanently?')">🗑️ Delete</a>
                    <a href="?action=unread&id=<?= $msg['id'] ?>" class="text-xs text-gray-500 hover:text-white flex items-center px-4">Mark as Unread</a>
                </div>
            </div>
        </div>
        <div>
            <div class="card-dark p-6">
                <h3 class="text-xs font-bold uppercase tracking-widest text-gray-500 mb-6">Sender Intelligence</h3>
                <div class="space-y-6">
                    <div>
                        <div class="text-[10px] text-gray-600 uppercase font-bold mb-1">Company</div>
                        <div class="text-sm text-white font-medium"><?= e($msg['company'] ?: 'Personal/N/A') ?></div>
                    </div>
                    <div>
                        <div class="text-[10px] text-gray-600 uppercase font-bold mb-1">Industry</div>
                        <div class="text-sm text-white font-medium"><?= e($msg['industry'] ?: 'Not Specified') ?></div>
                    </div>
                    <div>
                        <div class="text-[10px] text-gray-600 uppercase font-bold mb-1">Contact Email</div>
                        <a href="mailto:<?= e($msg['email']) ?>" class="text-sm text-orange-400 hover:underline"><?= e($msg['email']) ?></a>
                    </div>
                    <div>
                        <div class="text-[10px] text-gray-600 uppercase font-bold mb-1">Phone / WhatsApp</div>
                        <div class="text-sm text-white font-medium"><?= e($msg['phone'] ?: 'No Phone Provided') ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php
    include __DIR__ . '/includes/footer.php';
    exit;
    endif;
}

// Inbox List
$messages = Database::fetchAll("SELECT * FROM messages ORDER BY created_at DESC");
?>

<div class="max-w-5xl">
    <div class="flex items-center justify-between mb-8">
        <div>
            <h2 class="text-xl font-bold text-white">Inbox</h2>
            <p class="text-xs text-gray-500">Manage technical inquiries and contact form submissions</p>
        </div>
        <div class="text-xs text-gray-400">
            <?= count($messages) ?> Messages Total
        </div>
    </div>

    <div class="card-dark overflow-hidden">
        <?php if (empty($messages)): ?>
            <div class="p-20 text-center">
                <div class="text-6xl mb-4 opacity-20">📥</div>
                <div class="text-gray-500 font-bold uppercase tracking-widest text-xs">Inbox is Empty</div>
            </div>
        <?php else: ?>
            <div class="overflow-x-auto">
                <table class="w-full text-left text-sm">
                    <thead class="bg-navy-700/50 border-b border-navy-500">
                        <tr>
                            <th class="px-6 py-4 font-bold text-gray-400 uppercase text-[10px] tracking-wider">Status</th>
                            <th class="px-6 py-4 font-bold text-gray-400 uppercase text-[10px] tracking-wider">Sender</th>
                            <th class="px-6 py-4 font-bold text-gray-400 uppercase text-[10px] tracking-wider">Subject</th>
                            <th class="px-6 py-4 font-bold text-gray-400 uppercase text-[10px] tracking-wider">Date</th>
                            <th class="px-6 py-4"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-navy-500/50">
                        <?php foreach ($messages as $m): ?>
                        <tr class="<?= $m['is_read'] ? 'bg-transparent' : 'bg-orange-600/5' ?> hover:bg-navy-700/30 transition-colors">
                            <td class="px-6 py-4">
                                <?php if (!$m['is_read']): ?>
                                    <span class="inline-block w-2 h-2 rounded-full bg-orange-500 shadow-[0_0_8px_rgba(249,115,22,0.5)]"></span>
                                <?php else: ?>
                                    <span class="inline-block w-2 h-2 rounded-full bg-navy-500"></span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4">
                                <div class="font-bold text-white"><?= e($m['name']) ?></div>
                                <div class="text-[11px] text-gray-500"><?= e($m['email']) ?></div>
                            </td>
                            <td class="px-6 py-4 text-gray-300">
                                <div class="font-medium"><?= e($m['subject'] ?: 'General Inquiry') ?></div>
                                <div class="text-[11px] text-gray-500 truncate max-w-xs"><?= e(substr($m['message'], 0, 80)) ?>...</div>
                            </td>
                            <td class="px-6 py-4 text-[11px] text-gray-500">
                                <?= date('M d, Y', strtotime($m['created_at'])) ?>
                            </td>
                            <td class="px-6 py-4 text-right">
                                <a href="?view=<?= $m['id'] ?>" class="text-orange-500 hover:text-orange-400 font-bold text-[11px] uppercase tracking-widest">Open →</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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