<?php
/**
 * Agentic Square - Admin Support Ticket Center
 * Path: admin/support-center.php
 */

require_once __DIR__ . '/../config/config.php';

if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] ?? '') !== 'admin') {
    header('Location: ../login.php');
    exit;
}

$tickets = [];
$error_message = '';

try {
    $stmt = $pdo->query("
        SELECT
            st.*,
            u.full_name AS client_name,
            u.email AS client_email,
            o.item_name
        FROM support_tickets st
        LEFT JOIN users u ON st.user_id = u.id
        LEFT JOIN orders o ON st.order_id = o.id
        ORDER BY st.created_at DESC
        LIMIT 100
    ");
    $tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Throwable $e) {
    $error_message = 'Support ticket table is not available yet: ' . $e->getMessage();
}

$page_title = 'Support Center';
require_once __DIR__ . '/../sections/header.php';
?>

<body class="bg-nftDark text-white antialiased overflow-x-hidden">
    <?php include_once __DIR__ . '/admin-sidebar.php'; ?>

    <main class="lg:ml-64 min-h-screen pt-24 lg:pt-10 pb-16 px-4 sm:px-6 lg:px-8">
        <div class="max-w-7xl mx-auto space-y-8">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 border-b border-slate-900/60 pb-6">
                <div>
                    <span class="text-xs font-bold tracking-widest text-nftAccentCyan uppercase block mb-1">// Relations & Trust</span>
                    <h1 class="text-3xl font-extrabold tracking-tight text-white">Support Center</h1>
                    <p class="text-sm text-nftTextGray mt-1">Review client support tickets across the marketplace.</p>
                </div>
            </div>

            <?php if ($error_message): ?>
                <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-xs font-mono">
                    <?php echo htmlspecialchars($error_message, ENT_QUOTES, 'UTF-8'); ?>
                </div>
            <?php endif; ?>

            <div class="glass-card rounded-2xl border border-slate-800/80 bg-slate-950/20 overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="border-b border-slate-900 bg-slate-950/40 text-xs font-bold uppercase text-nftTextGray tracking-wider">
                                <th class="p-4">Date</th>
                                <th class="p-4">Client</th>
                                <th class="p-4">Item</th>
                                <th class="p-4">Subject</th>
                                <th class="p-4">Priority</th>
                                <th class="p-4">Status</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-900/60 text-sm">
                            <?php if (empty($tickets)): ?>
                                <tr>
                                    <td colspan="6" class="p-8 text-center text-nftTextGray font-mono text-xs">
                                        No support tickets found.
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($tickets as $ticket): ?>
                                    <tr class="hover:bg-slate-900/20 transition-all">
                                        <td class="p-4 text-slate-400 font-mono text-xs"><?php echo htmlspecialchars(date('M d, Y', strtotime($ticket['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8'); ?></td>
                                        <td class="p-4">
                                            <div class="font-semibold text-white"><?php echo htmlspecialchars($ticket['client_name'] ?? 'Unknown Client', ENT_QUOTES, 'UTF-8'); ?></div>
                                            <div class="text-[11px] text-slate-500"><?php echo htmlspecialchars($ticket['client_email'] ?? '', ENT_QUOTES, 'UTF-8'); ?></div>
                                        </td>
                                        <td class="p-4 text-slate-300"><?php echo htmlspecialchars($ticket['item_name'] ?? 'N/A', ENT_QUOTES, 'UTF-8'); ?></td>
                                        <td class="p-4 text-slate-300 max-w-xs truncate" title="<?php echo htmlspecialchars($ticket['subject'] ?? '', ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($ticket['subject'] ?? '', ENT_QUOTES, 'UTF-8'); ?></td>
                                        <td class="p-4">
                                            <span class="px-2.5 py-1 rounded-md text-xs font-bold uppercase <?php echo (($ticket['priority'] ?? '') === 'High') ? 'bg-rose-500/10 text-rose-400 border border-rose-500/20' : 'bg-slate-800 text-slate-400'; ?>">
                                                <?php echo htmlspecialchars($ticket['priority'] ?? 'Low', ENT_QUOTES, 'UTF-8'); ?>
                                            </span>
                                        </td>
                                        <td class="p-4">
                                            <span class="px-2.5 py-1 rounded-md text-xs font-bold uppercase <?php echo (($ticket['ticket_status'] ?? '') === 'Solved') ? 'bg-emerald-500/10 text-emerald-400 border border-emerald-500/20' : 'bg-amber-500/10 text-amber-400 border border-amber-500/20'; ?>">
                                                <?php echo htmlspecialchars($ticket['ticket_status'] ?? 'Pending', ENT_QUOTES, 'UTF-8'); ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
</body>
</html>
