<?php
$pageTitle = "Hall Details";
require_once 'includes/header.php';

$hall_id = (int)($_GET['id'] ?? 0);
$result = mysqli_query($conn, "SELECT h.*, u.name as vendor_name, u.phone as vendor_phone 
                               FROM halls h LEFT JOIN users u ON h.vendor_id = u.id 
                               WHERE h.id = $hall_id");

if (!$result || mysqli_num_rows($result) === 0) {
    echo "<div class='container' style='padding:60px 20px;'><div class='alert alert-danger'>Hall not found.</div></div>";
    require_once 'includes/footer.php';
    exit();
}

$hall = mysqli_fetch_assoc($result);
$gallery = mysqli_query($conn, "SELECT * FROM hall_gallery WHERE hall_id = $hall_id");
$dishes = mysqli_query($conn, "SELECT * FROM dishes WHERE hall_id = $hall_id ORDER BY category");
$dates = mysqli_query($conn, "SELECT * FROM hall_dates WHERE hall_id = $hall_id AND available_date >= CURDATE() ORDER BY available_date ASC");
$availCount = 0; $bookedCount = 0;
$datesArray = [];
while ($d = mysqli_fetch_assoc($dates)) {
    $datesArray[] = $d;
    if ($d['status'] === 'available') $availCount++;
    else $bookedCount++;
}
?>

<!-- Hall Hero -->
<section class="hall-hero">
    <img src="<?php echo $hall['main_image'] ? BASE_URL.'/assets/uploads/halls/'.$hall['main_image'] : 'https://images.unsplash.com/photo-1519167758481-83f550bb49b3?w=1920'; ?>" 
         alt="<?php echo htmlspecialchars($hall['hall_name']); ?>" class="hall-hero-img">
    <div class="hall-hero-overlay">
        <div class="hall-hero-content">
            <p class="location">✦ <?php echo htmlspecialchars($hall['location']); ?></p>
            <h1><?php echo htmlspecialchars($hall['hall_name']); ?></h1>
            <p style="font-family:var(--font-serif); font-style:italic; font-size:18px; color:rgba(255,255,255,0.9);">
                A distinguished venue by <?php echo htmlspecialchars($hall['vendor_name']); ?>
            </p>
        </div>
    </div>
</section>

<div class="container" style="padding-top:20px;">
    <?php showFlash(); ?>
</div>

<div class="hall-detail-grid">
    <!-- Left: Main Content -->
    <div>
        <div class="hall-info-box">
            <h2>About This Venue</h2>
            <p style="color:#666; font-size:16px; line-height:1.9; margin-top:15px;">
                <?php echo nl2br(htmlspecialchars($hall['description'] ?: 'A beautiful venue for your special day.')); ?>
            </p>
        </div>

        <!-- Video -->
        <?php if (!empty($hall['video'])): ?>
        <div class="hall-info-box" style="margin-top:30px;">
            <h3>Virtual Tour</h3>
            <div class="video-wrapper">
                <video controls>
                    <source src="<?php echo BASE_URL.'/assets/uploads/videos/'.$hall['video']; ?>" type="video/mp4">
                </video>
            </div>
        </div>
        <?php endif; ?>

        <!-- Gallery -->
        <?php if (mysqli_num_rows($gallery) > 0): ?>
        <div class="hall-info-box" style="margin-top:30px;">
            <h3>Gallery</h3>
            <div class="gallery">
                <?php while ($img = mysqli_fetch_assoc($gallery)): ?>
                    <div class="gallery-item" onclick="window.open('<?php echo BASE_URL.'/assets/uploads/halls/'.$img['image']; ?>', '_blank')">
                        <img src="<?php echo BASE_URL.'/assets/uploads/halls/'.$img['image']; ?>" alt="Gallery">
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Dishes -->
        <div class="hall-info-box" style="margin-top:30px;">
            <h3>Menu & Pricing</h3>
            <?php if (mysqli_num_rows($dishes) > 0): ?>
                <div class="dishes-list">
                    <?php while ($d = mysqli_fetch_assoc($dishes)): ?>
                        <div class="dish-item">
                            <div class="dish-item-info">
                                <p class="category"><?php echo htmlspecialchars($d['category']); ?></p>
                                <h4><?php echo htmlspecialchars($d['dish_name']); ?></h4>
                                <p><?php echo htmlspecialchars($d['description']); ?></p>
                            </div>
                            <div class="dish-price">Rs. <?php echo number_format($d['price']); ?></div>
                        </div>
                    <?php endwhile; ?>
                </div>
            <?php else: ?>
                <p style="color:#888; padding:20px 0;">Menu details coming soon.</p>
            <?php endif; ?>
        </div>

        <!-- Dates -->
        <div class="hall-info-box" style="margin-top:30px;">
            <h3>Availability Calendar</h3>
            <div style="display:flex; gap:20px; margin-bottom:15px; flex-wrap:wrap;">
                <span><span class="badge badge-available">Available (<?php echo $availCount; ?>)</span></span>
                <span><span class="badge badge-booked">Booked (<?php echo $bookedCount; ?>)</span></span>
            </div>
            <?php if (count($datesArray) > 0): ?>
                <div class="date-grid">
                    <?php foreach ($datesArray as $d): ?>
                        <div class="date-cell <?php echo $d['status']; ?>">
                            <?php echo date('d M', strtotime($d['available_date'])); ?>
                            <small><?php echo date('Y', strtotime($d['available_date'])); ?></small>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <p style="color:#888;">No dates added by vendor yet.</p>
            <?php endif; ?>
        </div>
    </div>

    <!-- Right: Sticky Booking Card -->
    <div>
        <div class="booking-card">
            <div class="price-tag">
                <p class="label">Starting From</p>
                <p class="amount">Rs. <?php echo number_format($hall['price_per_day']); ?><br><small>per day</small></p>
            </div>
            
            <ul class="details-list">
                <li>Capacity <span><?php echo $hall['capacity']; ?> guests</span></li>
                <li>Available Dates <span><?php echo $availCount; ?></span></li>
                <li>Menu Items <span><?php echo mysqli_num_rows($dishes); ?></span></li>
                <li>Vendor <span><?php echo htmlspecialchars($hall['vendor_name']); ?></span></li>
            </ul>
            
            <?php if (isLoggedIn() && getUserRole() === 'user'): ?>
                <a href="<?php echo BASE_URL; ?>/user/book.php?hall_id=<?php echo $hall['id']; ?>" class="btn">Request Booking</a>
                <div style="text-align:center; margin-top:15px;">
                    <a href="<?php echo BASE_URL; ?>/user/contact-admin.php?hall_id=<?php echo $hall['id']; ?>" 
                       style="color:var(--gold); text-decoration:none; font-size:13px; letter-spacing:1px; text-transform:uppercase;">
                        💬 Contact Admin
                    </a>
                </div>
            <?php elseif (!isLoggedIn()): ?>
                <a href="<?php echo BASE_URL; ?>/login.php" class="btn">Login to Book</a>
                <p style="text-align:center; margin-top:15px; font-size:13px; color:rgba(255,255,255,0.7);">
                    New here? <a href="<?php echo BASE_URL; ?>/register.php" style="color:var(--gold);">Register</a>
                </p>
            <?php else: ?>
                <p style="text-align:center; color:rgba(255,255,255,0.8); font-size:13px;">
                    Only users can book. Switch to a user account.
                </p>
            <?php endif; ?>
        </div>
    </div>
</div>

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