<?php
// File: /artist/competitions.php (Corrected & Final Version)
include 'header.php'; // Includes security check and DB connection

$artist_id = $_SESSION['user_id'];
$competitions = [];

// **THE FIX IS HERE**: The subquery now correctly uses "ce.artist_id" to match your database table.
$sql = "SELECT 
            c.id, 
            c.title, 
            c.description, 
            c.end_date, 
            c.competition_fee,
            (SELECT COUNT(*) FROM competition_entries ce WHERE ce.competition_id = c.id AND ce.artist_id = ?) as has_entered
        FROM competitions c
        WHERE c.is_active = 1
        ORDER BY c.end_date ASC";

$stmt = $conn->prepare($sql);
if ($stmt) {
    $stmt->bind_param("i", $artist_id);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result) {
        $competitions = $result->fetch_all(MYSQLI_ASSOC);
    }
    $stmt->close();
}
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Art Competitions</h1>

    <?php if (!empty($competitions)): ?>
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
            <?php foreach ($competitions as $comp): 
                $is_open = strtotime($comp['end_date']) > time();
            ?>
                <div class="bg-white rounded-lg shadow-lg overflow-hidden flex flex-col">
                    <div class="p-6 flex-grow">
                        <h2 class="text-2xl font-bold text-ucf-charcoal mb-2"><?php echo htmlspecialchars($comp['title']); ?></h2>
                        <div class="flex items-center space-x-4 text-sm text-gray-500 mb-4">
                            <span>
                                <i class="fas fa-calendar-day mr-1"></i>
                                Closes: <?php echo date('M d, Y', strtotime($comp['end_date'])); ?>
                            </span>
                            <span>
                                <i class="fas fa-rupee-sign mr-1"></i>
                                Fee: <?php echo number_format($comp['competition_fee'], 2); ?>
                            </span>
                        </div>
                        <p class="text-gray-600 mb-4 line-clamp-3">
                            <?php echo htmlspecialchars($comp['description']); ?>
                        </p>
                    </div>
                    <div class="p-6 bg-gray-50">
                        <?php if ($comp['has_entered']): ?>
                            <span class="w-full block text-center rounded-md border border-transparent bg-green-600 px-6 py-3 text-base font-medium text-white">
                               <i class="fas fa-check-circle mr-2"></i> You have Entered
                            </span>
                        <?php elseif ($is_open): ?>
                            <a href="competition_entry.php?id=<?php echo $comp['id']; ?>" class="w-full block text-center rounded-md border border-transparent bg-ucf-green px-6 py-3 text-base font-medium text-white shadow-sm hover:bg-ucf-green-dark">
                                Participate Now
                            </a>
                        <?php else: ?>
                            <span class="w-full block text-center rounded-md border border-transparent bg-gray-400 px-6 py-3 text-base font-medium text-white">
                                Competition Closed
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="text-center bg-white p-12 rounded-lg shadow-md">
            <i class="fas fa-trophy fa-4x text-gray-300 mb-4"></i>
            <h2 class="text-2xl font-bold text-ucf-charcoal">No Active Competitions</h2>
            <p class="text-gray-500 mt-2">There are no competitions running at the moment. Please check back soon!</p>
        </div>
    <?php endif; ?>
</div>

<?php include 'footer.php'; ?>

