<?php
include 'includes/auth.php';
include 'db.php';

/* ================= DASHBOARD ================= */
$dashboard_title = 'Kitchen Dashboard';
$dashboard_icon  = 'fa-hat-chef';

/* ================= SUMMARY STATS ================= */
$todayMeals = $conn->query("
    SELECT COUNT(*) AS c 
    FROM walkin_production  
    WHERE DATE(production_date)=CURDATE()
")->fetch_assoc()['c'] ?? 0;

$totalMeals = $conn->query("
    SELECT COUNT(*) AS c 
    FROM walkin_production
")->fetch_assoc()['c'] ?? 0;

$totalPeople = $conn->query("
    SELECT COALESCE(SUM(people_count),0) AS c 
    FROM walkin_production
")->fetch_assoc()['c'] ?? 0;

/* ================= NOTIFICATIONS ================= */

// 🔴 Restaurant Orders (not paid)
$restaurant_orders = $conn->query("
    SELECT COUNT(*) AS c 
    FROM restaurant_orders 
    WHERE status != 'PAID'
")->fetch_assoc()['c'] ?? 0;

// 🥞 Breakfast Orders (pending + current guests only)
$breakfast_orders = $conn->query("
    SELECT COUNT(*) AS c 
    FROM stays s
    JOIN guests g ON s.guest_id = g.id
    WHERE s.checkout_date IS NULL
    AND g.name <> 'Walk-in'
    AND s.breakfast_prepared = 0
")->fetch_assoc()['c'] ?? 0;

/* ================= MODULE CARDS ================= */
$module_cards = [

    ['link'=>'conference_meals.php', 'icon'=>'fa-utensils', 'title'=>'Conference Meals Production/Requisitions', 'color'=>'bg-success'],

    ['link'=>'wperishable.php', 'icon'=>'fa-utensils', 'title'=>'Conference Perishables Reports', 'color'=>'bg-warning text-dark'],

    ['link'=>'wstock_items_report.php', 'icon'=>'fa-utensils', 'title'=>'Conference Stock Items Reports', 'color'=>'bg-info text-white'],

    ['link'=>'walk_in_meals.php', 'icon'=>'fa-hat-chef', 'title'=>'Walk-in Meal Production/Requisitions', 'color'=>'bg-warning text-dark'],

    ['link'=>'perishable.php', 'icon'=>'fa-hat-chef', 'title'=>'Walkin Perishables Reports', 'color'=>'bg-info text-white'],

    ['link'=>'stock_items_report.php', 'icon'=>'fa-hat-chef', 'title'=>'Walkin Stock Items Reports', 'color'=>'bg-info text-white'],

    // 🥞 Breakfast Orders (WITH NOTIFICATION)
    [
        'link'=>'breakfast_orders_list.php',
        'icon'=>'fa-bread-slice',
        'title'=>'Breakfast Orders',
        'color'=>'bg-success',
        'notify' => $breakfast_orders
    ],

    ['link'=>'restaurant_sales_list.php', 'icon'=>'fa-shopping-basket', 'title'=>'Restaurant Sales', 'color'=>'bg-primary'],

    // 🔴 Restaurant Orders (WITH NOTIFICATION)
    [
        'link'=>'restaurant_orders_list.php',
        'icon'=>'fa-shopping-basket',
        'title'=>'Restaurant Orders',
        'color'=>'bg-primary',
        'notify' => $restaurant_orders
    ],

    ['link'=>'kitchen_inventory.php', 'icon'=>'fa-warehouse', 'title'=>'Kitchen Inventory', 'color'=>'bg-dark'],

    ['link'=>'kitchen_inventory.php', 'icon'=>'fa-warehouse', 'title'=>'Kitchen Requisitions', 'color'=>'bg-warning'],

    ['link'=>'kitchen_req_reports.php', 'icon'=>'fa-warehouse', 'title'=>'Kitchen Requisition Reports', 'color'=>'bg-dark'],

    ['link'=>'kitchen_reports.php', 'icon'=>'fa-chart-bar', 'title'=>'Reports', 'color'=>'bg-dark text-white'],
];

include 'includes/header.php';
?>

<style>
.card-module {
    width: 120px;
    text-align: center;
    display: inline-block;
    border-radius: 8px;
    transition: 0.2s;
    cursor: pointer;
    padding: 5px;
    position: relative;
}

.card-module:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.12);
}

.card-module a {
    text-decoration: none;
    color: inherit;
}

.icon-box {
    font-size: 22px;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    margin: 0 auto 8px auto;
    color: #fff;
}

.flex-center {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 15px;
}

.welcome-msg {
    font-size: 1rem;
    margin-bottom: 15px;
    color: #1b4d32;
}

.card-body h6 {
    font-size: 0.85rem;
}

/* 🔴 notification badge */
.badge-notify {
    position: absolute;
    top: 5px;
    right: 8px;
    background: red;
    color: white;
    font-size: 11px;
    padding: 3px 6px;
    border-radius: 50px;
}
</style>

<div class="container-fluid mt-4">

    <button class="btn btn-secondary mb-3" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>

    <div class="welcome-msg">
        <i class="fa fa-user-circle"></i>
        Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>

    <h3 class="mb-3">
        <i class="fa <?= $dashboard_icon ?> text-success"></i> <?= $dashboard_title ?>
    </h3>

    <hr>

    <!-- ================= SUMMARY ================= -->
    <div class="row mb-3 text-center">

        <div class="col-md-4">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Today's Productions</h6>
                    <h3 class="text-success"><?= $todayMeals ?></h3>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Total Productions</h6>
                    <h3 class="text-primary"><?= $totalMeals ?></h3>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card shadow-sm">
                <div class="card-body p-2">
                    <h6>Total People Served</h6>
                    <h3 class="text-dark"><?= number_format($totalPeople) ?></h3>
                </div>
            </div>
        </div>

    </div>

    <!-- ================= MODULE CARDS ================= -->
    <div class="flex-center">

        <?php foreach($module_cards as $card): ?>
            <div class="card card-module shadow-sm">

                <?php if(!empty($card['notify']) && $card['notify'] > 0): ?>
                    <span class="badge-notify">
                        <?= $card['notify'] ?>
                    </span>
                <?php endif; ?>

                <a href="<?= $card['link'] ?>">
                    <div class="card-body p-2">
                        <div class="icon-box <?= $card['color'] ?>">
                            <i class="fa <?= $card['icon'] ?>"></i>
                        </div>
                        <h6><?= $card['title'] ?></h6>
                    </div>
                </a>

            </div>
        <?php endforeach; ?>

    </div>

</div>

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