<?php
include 'header.php';
require_once '../db/config.php';

// Check if the user is logged in as an admin
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

$pdo = db();

// Analytics Queries
// Total Revenue
$stmt_revenue = $pdo->prepare("SELECT SUM(total_price) as total_revenue FROM orders WHERE status = ?");
$stmt_revenue->execute(['completed']);
$total_revenue = $stmt_revenue->fetchColumn();

// Total Orders
$total_orders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();

// Total Customers
$total_customers = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'customer'")->fetchColumn();

// Most Popular Restaurants
$stmt_popular = $pdo->query("
    SELECT r.name, COUNT(o.id) as order_count
    FROM restaurants r
    JOIN orders o ON r.id = o.restaurant_id
    GROUP BY r.id
    ORDER BY order_count DESC
    LIMIT 5
");
$popular_restaurants = $stmt_popular->fetchAll();

// Fetch all restaurants for the management table
$stmt_restaurants = $pdo->query("SELECT id, name, cuisine FROM restaurants ORDER BY name");
$restaurants = $stmt_restaurants->fetchAll();
?>

<div class="container mt-4">
    <h2>Admin Dashboard</h2>

    <!-- Analytics Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card text-white bg-primary mb-3">
                <div class="card-header">Total Revenue</div>
                <div class="card-body">
                    <h5 class="card-title">$<?php echo number_format($total_revenue, 2); ?></h5>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-white bg-info mb-3">
                <div class="card-header">Total Orders</div>
                <div class="card-body">
                    <h5 class="card-title"><?php echo $total_orders; ?></h5>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-white bg-success mb-3">
                <div class="card-header">Total Customers</div>
                <div class="card-body">
                    <h5 class="card-title"><?php echo $total_customers; ?></h5>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    Most Popular Restaurants
                </div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($popular_restaurants as $restaurant): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <?php echo htmlspecialchars($restaurant['name']); ?>
                            <span class="badge bg-primary rounded-pill"><?php echo $restaurant['order_count']; ?> orders</span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>


    <h2 class="mt-5">Restaurant Management</h2>
    <p><a href="add_restaurant.php" class="btn btn-success">Add New Restaurant</a></p>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Cuisine</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($restaurants as $restaurant): ?>
                <tr>
                    <td><?php echo htmlspecialchars($restaurant['name']); ?></td>
                    <td><?php echo htmlspecialchars($restaurant['cuisine']); ?></td>
                    <td>
                        <a href="menu.php?restaurant_id=<?php echo $restaurant['id']; ?>" class="btn btn-info btn-sm">Manage Menu</a>
                        <a href="edit_restaurant.php?id=<?php echo $restaurant['id']; ?>" class="btn btn-primary btn-sm">Edit</a>
                        <a href="delete_restaurant.php?id=<?php echo $restaurant['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this restaurant?');">Delete</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

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