<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/layout.php';

$error = '';
$success = '';

// Optional filter by park
$parkFilter = $_GET['park_id'] ?? '';
$parkFilter = ($parkFilter !== '') ? (int)$parkFilter : '';

// Fetch parks for filter dropdown
$parksStmt = $pdo->query("SELECT id, name FROM parks ORDER BY name ASC");
$parks = $parksStmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch rides (join park + area names)
$sql = "
  SELECT 
    r.*,
    p.name AS park_name,
    a.name AS area_name
  FROM rides r
  INNER JOIN parks p ON p.id = r.park_id
  LEFT JOIN park_areas a ON a.id = r.area_id
";
$params = [];

if ($parkFilter !== '') {
  $sql .= " WHERE r.park_id = ? ";
  $params[] = $parkFilter;
}

$sql .= " ORDER BY p.name ASC, r.name ASC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rides = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h1>Rides</h1>

<div class="d-flex align-items-center justify-content-between mb-3">
  <form method="get" class="d-flex gap-2 align-items-center">
    <label class="form-label mb-0" for="park_id">Filter by Park</label>
    <select name="park_id" id="park_id" class="form-control" style="max-width:320px;">
      <option value="">All Parks</option>
      <?php foreach ($parks as $p): ?>
        <option value="<?= (int)$p['id'] ?>" <?= ($parkFilter !== '' && (int)$parkFilter === (int)$p['id']) ? 'selected' : '' ?>>
          <?= htmlspecialchars($p['name']) ?>
        </option>
      <?php endforeach; ?>
    </select>
    <button type="submit" class="btn btn-outline-secondary">Apply</button>
    <?php if ($parkFilter !== ''): ?>
      <a class="btn btn-link" href="rides.php">Clear</a>
    <?php endif; ?>
  </form>

  <a href="ride_form.php" class="btn btn-primary">+ Create New Ride</a>
</div>

<?php if (empty($rides)): ?>
  <div class="alert alert-info">No rides found.</div>
<?php else: ?>
  <div class="table-responsive">
    <table class="table table-striped align-middle">
      <thead>
        <tr>
          <th>Ride</th>
          <th>Park</th>
          <th>Area</th>
          <th>Thrill</th>
          <th>Min Height</th>
          <th>Avg Wait</th>
          <th>Rating</th>
          <th>Featured</th>
          <th style="width:140px;"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rides as $r): ?>
          <tr>
            <td>
              <div class="fw-semibold"><?= htmlspecialchars($r['name']) ?></div>
              <?php if (!empty($r['image_path'])): ?>
                <small class="text-muted">Has image</small>
              <?php endif; ?>
            </td>
            <td><?= htmlspecialchars($r['park_name']) ?></td>
            <td><?= htmlspecialchars($r['area_name'] ?? '—') ?></td>
            <td><?= htmlspecialchars($r['thrill_level'] ?? '—') ?></td>
            <td><?= $r['min_height_cm'] !== null ? (int)$r['min_height_cm'] . ' cm' : '—' ?></td>
            <td><?= $r['avg_wait_time'] !== null ? (int)$r['avg_wait_time'] . ' min' : '—' ?></td>
            <td><?= $r['rating'] !== null ? htmlspecialchars($r['rating']) : '—' ?></td>
            <td><?= !empty($r['featured']) ? 'Yes' : 'No' ?></td>
            <td class="text-end">
              <a class="btn btn-sm btn-outline-primary" href="ride_form.php?id=<?= (int)$r['id'] ?>">Edit</a>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php endif; ?>