<?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();

// Fetch all cuisines
$cuisines_stmt = $pdo->query("SELECT * FROM cuisines ORDER BY name ASC");
$cuisines = $cuisines_stmt->fetchAll();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $description = $_POST['description'] ?? '';
    $image_url = $_POST['image_url'] ?? '';
    $selected_cuisines = $_POST['cuisines'] ?? [];

    if ($name && !empty($selected_cuisines)) {
        try {
            $pdo->beginTransaction();

            // Insert into restaurants table
            $stmt = $pdo->prepare("INSERT INTO restaurants (name, description, image_url) VALUES (?, ?, ?)");
            $stmt->execute([$name, $description, $image_url]);
            $restaurant_id = $pdo->lastInsertId();

            // Insert into restaurant_cuisines table
            $cuisine_stmt = $pdo->prepare("INSERT INTO restaurant_cuisines (restaurant_id, cuisine_id) VALUES (?, ?)");
            foreach ($selected_cuisines as $cuisine_id) {
                $cuisine_stmt->execute([$restaurant_id, $cuisine_id]);
            }

            $pdo->commit();
            header('Location: index.php');
            exit;
        } catch (Exception $e) {
            $pdo->rollBack();
            $error = "Error adding restaurant: " . $e->getMessage();
        }
    } else {
        $error = "Name and at least one cuisine are required.";
    }
}
?>

<div class="container mt-4">
    <h2>Add New Restaurant</h2>

    <?php if (isset($error)): ?>
        <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>

    <form action="add_restaurant.php" method="POST">
        <div class="mb-3">
            <label for="name" class="form-label">Name</label>
            <input type="text" class="form-control" id="name" name="name" required>
        </div>
        <div class="mb-3">
            <label for="description" class="form-label">Description</label>
            <textarea class="form-control" id="description" name="description"></textarea>
        </div>
        <div class="mb-3">
            <label class="form-label">Cuisines</label>
            <div class="row">
                <?php foreach ($cuisines as $cuisine): ?>
                    <div class="col-md-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" name="cuisines[]" value="<?php echo $cuisine['id']; ?>" id="cuisine_<?php echo $cuisine['id']; ?>">
                            <label class="form-check-label" for="cuisine_<?php echo $cuisine['id']; ?>">
                                <?php echo htmlspecialchars($cuisine['name']); ?>
                            </label>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="mb-3">
            <label for="image_url" class="form-label">Image URL</label>
            <input type="text" class="form-control" id="image_url" name="image_url">
        </div>
        <button type="submit" class="btn btn-primary">Add Restaurant</button>
        <a href="index.php" class="btn btn-secondary">Cancel</a>
    </form>
</div>

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