<?php
/**
 * Admin Dashboard — Overview & Quick Stats
 * Path: admin/index.php
 */
require_once 'layout-header.php';

$total_products  = count_products();
$total_reviews   = count_reviews();
$total_chats     = count_chat_messages();
$total_faqs      = count_faqs();
$total_categories= count_categories();
$total_inquiries = count_contact_inquiries();
$unread_inquiries= count_contact_inquiries(true);
$recent_chats    = get_recent_chat_history(5);
$site_name       = get_site_setting('site_name');
?>

<main class="flex-1 p-6 md:p-8 space-y-6">

    <!-- Page Header -->
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 border-b border-slate-800/80 pb-5">
        <div>
            <h1 class="text-2xl font-black text-white uppercase tracking-wider">Dashboard</h1>
            <p class="text-xs text-slate-400 mt-1"><?php echo htmlspecialchars($site_name); ?> — সাইট ওভারভিউ</p>
        </div>
        <div class="flex items-center gap-2 text-xs text-slate-500">
            <i class="fa-solid fa-clock"></i>
            <span><?php echo date('l, d M Y — h:i A'); ?></span>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="grid grid-cols-2 lg:grid-cols-3 xl:grid-cols-5 gap-4">

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-sky-500/30 transition-all group">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">Products</span>
                <div class="w-9 h-9 rounded-xl bg-sky-500/10 flex items-center justify-center group-hover:bg-sky-500/20 transition-all">
                    <i class="fa-solid fa-boxes-stacked text-sky-400"></i>
                </div>
            </div>
            <p class="text-3xl font-black text-white"><?php echo $total_products; ?></p>
            <p class="text-[10px] text-slate-500 mt-1"><?php echo $total_categories; ?> Categories</p>
        </div>

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-amber-500/30 transition-all group">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">Reviews</span>
                <div class="w-9 h-9 rounded-xl bg-amber-500/10 flex items-center justify-center group-hover:bg-amber-500/20 transition-all">
                    <i class="fa-solid fa-star text-amber-400"></i>
                </div>
            </div>
            <p class="text-3xl font-black text-white"><?php echo $total_reviews; ?></p>
            <p class="text-[10px] text-slate-500 mt-1">Client Testimonials</p>
        </div>

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-emerald-500/30 transition-all group">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">Chat Messages</span>
                <div class="w-9 h-9 rounded-xl bg-emerald-500/10 flex items-center justify-center group-hover:bg-emerald-500/20 transition-all">
                    <i class="fa-solid fa-comments text-emerald-400"></i>
                </div>
            </div>
            <p class="text-3xl font-black text-white"><?php echo $total_chats; ?></p>
            <p class="text-[10px] text-slate-500 mt-1">Total Conversations</p>
        </div>

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-purple-500/30 transition-all group">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">FAQs</span>
                <div class="w-9 h-9 rounded-xl bg-purple-500/10 flex items-center justify-center group-hover:bg-purple-500/20 transition-all">
                    <i class="fa-solid fa-circle-question text-purple-400"></i>
                </div>
            </div>
            <p class="text-3xl font-black text-white"><?php echo $total_faqs; ?></p>
            <p class="text-[10px] text-slate-500 mt-1">Published Questions</p>
        </div>

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-rose-500/30 transition-all group col-span-2 lg:col-span-1">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">Quote Requests</span>
                <div class="w-9 h-9 rounded-xl bg-rose-500/10 flex items-center justify-center">
                    <i class="fa-solid fa-envelope-open-text text-rose-400"></i>
                </div>
            </div>
            <p class="text-3xl font-black text-white"><?php echo $total_inquiries; ?></p>
            <p class="text-[10px] text-slate-500 mt-1"><?php echo $unread_inquiries; ?> unread</p>
        </div>

        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-5 hover:border-rose-500/30 transition-all group col-span-2 lg:col-span-1">
            <div class="flex items-center justify-between mb-3">
                <span class="text-[10px] font-bold uppercase text-slate-500 tracking-wider">DB Status</span>
                <div class="w-9 h-9 rounded-xl bg-rose-500/10 flex items-center justify-center">
                    <i class="fa-solid fa-database text-rose-400"></i>
                </div>
            </div>
            <p class="text-lg font-black <?php echo $conn ? 'text-emerald-400' : 'text-red-400'; ?>">
                <?php echo $conn ? 'Connected' : 'Offline'; ?>
            </p>
            <p class="text-[10px] text-slate-500 mt-1">MySQL Database</p>
        </div>
    </div>

    <!-- Quick Actions + Recent Chats -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">

        <!-- Quick Links -->
        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-6">
            <h2 class="text-sm font-bold uppercase text-sky-400 tracking-wider mb-4 flex items-center">
                <i class="fa-solid fa-bolt mr-2"></i> Quick Actions
            </h2>
            <div class="grid grid-cols-2 gap-3">
                <?php
                $quick_links = [
                    ['banner.php',       'fa-image',              'Banner',    'sky'],
                    ['inquiries.php',    'fa-envelope-open-text', 'Quotes',    'rose'],
                    ['products.php',     'fa-boxes-stacked',      'Products',  'amber'],
                    ['chatbot.php',      'fa-robot',              'Chatbot',   'emerald'],
                    ['reviews.php',      'fa-star',               'Reviews',   'yellow'],
                    ['chat-history.php', 'fa-comments',           'History',   'purple'],
                ];
                foreach ($quick_links as $ql):
                ?>
                <a href="<?php echo $ql[0]; ?>" class="flex items-center gap-3 p-3 rounded-xl bg-slate-950 border border-slate-800 hover:border-<?php echo $ql[3]; ?>-500/40 hover:bg-slate-800/50 transition-all group">
                    <i class="fa-solid <?php echo $ql[1]; ?> text-<?php echo $ql[3]; ?>-400 group-hover:scale-110 transition-transform"></i>
                    <span class="text-xs font-bold text-slate-300"><?php echo $ql[2]; ?></span>
                </a>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Recent Chat History -->
        <div class="bg-slate-900 border border-slate-800/80 rounded-2xl p-6">
            <div class="flex items-center justify-between mb-4">
                <h2 class="text-sm font-bold uppercase text-sky-400 tracking-wider flex items-center">
                    <i class="fa-solid fa-clock-rotate-left mr-2"></i> Recent Chats
                </h2>
                <a href="chat-history.php" class="text-[10px] font-bold text-sky-400 hover:text-sky-300 uppercase tracking-wider">View All →</a>
            </div>

            <?php if (empty($recent_chats)): ?>
                <div class="text-center py-8 text-slate-500">
                    <i class="fa-solid fa-comment-slash text-2xl mb-2 block opacity-40"></i>
                    <p class="text-xs">এখনো কোনো চ্যাট হিস্টরি নেই।</p>
                </div>
            <?php else: ?>
                <div class="space-y-3 max-h-72 overflow-y-auto pr-1">
                    <?php foreach ($recent_chats as $chat): ?>
                    <div class="bg-slate-950 border border-slate-800 rounded-xl p-3 space-y-1.5">
                        <div class="flex items-center justify-between">
                            <span class="text-[10px] text-slate-500 font-mono"><?php echo htmlspecialchars($chat['user_ip'] ?? '—'); ?></span>
                            <span class="text-[10px] text-slate-600"><?php echo date('d M, h:i A', strtotime($chat['created_at'])); ?></span>
                        </div>
                        <p class="text-xs text-sky-300 font-semibold truncate">
                            <i class="fa-solid fa-user text-[9px] mr-1"></i>
                            <?php echo htmlspecialchars(mb_substr($chat['user_message'], 0, 80)); ?>
                        </p>
                        <p class="text-[11px] text-slate-400 truncate">
                            <i class="fa-solid fa-robot text-[9px] mr-1"></i>
                            <?php echo htmlspecialchars(strip_tags(mb_substr($chat['bot_response'], 0, 80))); ?>
                        </p>
                    </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Setup Reminder -->
    <?php if (!$conn): ?>
    <div class="p-4 bg-red-950/40 border border-red-800 text-red-200 text-xs rounded-xl flex items-center">
        <i class="fa-solid fa-triangle-exclamation mr-3 text-base"></i>
        ডাটাবেজ কানেক্টেড নেই। config.php চেক করুন এবং <strong>install-three.php</strong> রান করুন।
    </div>
    <?php elseif ($total_products === 0): ?>
    <div class="p-4 bg-amber-950/40 border border-amber-800 text-amber-200 text-xs rounded-xl flex items-center">
        <i class="fa-solid fa-circle-info mr-3 text-base"></i>
        প্রডাক্ট ডেটা নেই। ব্রাউজারে <strong>install-three.php</strong> ওপেন করে ডিফল্ট ডেটা সিড করুন।
    </div>
    <?php endif; ?>

</main>

<?php require_once 'layout-footer.php'; ?>
