<?php
// File: artist.php (Fixed)

// --- STEP 1: FORCE ERROR DISPLAY ---
ini_set('display_errors', 1);
error_reporting(E_ALL);

include 'header.php'; // Includes session_start() and db.php

// --- 1. Get Artist ID from URL ---
$artist_id = (int)($_GET['id'] ?? 0);
if ($artist_id === 0) {
    die("Error: No artist ID provided.");
}

// --- 2. Fetch This Artist's Full Details ---
// ⭐️ FIX: Removed the 'ap.bio' column from the query
$stmt = $conn->prepare("
    SELECT 
        u.id as user_id, 
        u.email,
        ap.first_name, 
        ap.last_name, 
        ap.shop_name, 
        ap.phone, 
        ap.address, 
        ap.profile_image_path
    FROM users u
    JOIN artist_profiles ap ON u.id = ap.user_id
    WHERE u.id = ? AND u.role = 'artist' AND ap.subscription_status = 'active'
");
$stmt->bind_param("i", $artist_id);
$stmt->execute();
$result = $stmt->get_result();
$artist = $result->fetch_assoc();
$stmt->close();

if (!$artist) {
    // This will happen if the ID is invalid or the artist's subscription is not active
    die("Artist not found or is not active.");
}

// --- 3. Check Follow Status for This Artist ---
$is_following = false;
if (isset($_SESSION['user_id'])) {
    $current_user_id = $_SESSION['user_id'];
    $stmt_follow = $conn->prepare("SELECT 1 FROM followers WHERE customer_id = ? AND artist_id = ?");
    $stmt_follow->bind_param("ii", $current_user_id, $artist_id);
    $stmt_follow->execute();
    $is_following = $stmt_follow->get_result()->num_rows > 0;
    $stmt_follow->close();
}
?>

<div class="bg-gray-50 py-12 md:py-20">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        
        <!-- The "Big Card" -->
        <div class="bg-white rounded-xl shadow-2xl overflow-hidden">
            <div class="md:flex">
                <!-- Left Side: Big Profile Picture -->
                <div class="md:flex-shrink-0">
                    <img class="h-48 w-full object-cover md:h-full md:w-64" 
                         src="<?php echo htmlspecialchars($artist['profile_image_path'] ?? '/images/default_avatar.png'); ?>" 
                         alt="<?php echo htmlspecialchars($artist['shop_name']); ?>">
                </div>
                
                <!-- Right Side: Details -->
                <div class="p-8 flex-grow">
                    <h1 class="text-4xl font-extrabold text-ucf-charcoal">
                        <?php echo htmlspecialchars($artist['shop_name']); ?>
                    </h1>
                    <h2 class="text-xl font-medium text-gray-500 mt-1">
                        <?php echo htmlspecialchars(($artist['first_name'] ?? '') . ' ' . ($artist['last_name'] ?? '')); ?>
                    </h2>

                    <div class="mt-6 border-t border-gray-200 pt-6">
                        <dl class="grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-2">
                            <div class="sm:col-span-1">
                                <dt class="text-sm font-medium text-gray-500">
                                    <i class="fas fa-map-marker-alt mr-2 text-ucf-green"></i>Address
                                </dt>
                                <dd class="mt-1 text-base text-gray-900">
                                    <?php echo htmlspecialchars($artist['address'] ?? 'N/A'); ?>
                                </dd>
                            </div>
                            <div class="sm:col-span-1">
                                <dt class="text-sm font-medium text-gray-500">
                                    <i class="fas fa-phone-alt mr-2 text-ucf-green"></i>Phone
                                </dt>
                                <dd class="mt-1 text-base text-gray-900">
                                    <?php echo htmlspecialchars($artist['phone'] ?? 'N/A'); ?>
                                </dd>
                            </div>
                            <div class="sm:col-span-2">
                                <dt class="text-sm font-medium text-gray-500">
                                    <i class="fas fa-envelope mr-2 text-ucf-green"></i>Email
                                </dt>
                                <dd class="mt-1 text-base text-gray-900">
                                    <?php echo htmlspecialchars($artist['email'] ?? 'N/A'); ?>
                                </dd>
                            </div>
                            <!-- ⭐️ FIX: Removed the "About the Artist" section that used 'bio' -->
                        </dl>
                    </div>

                    <!-- Follow Button -->
                    <div class="mt-6 pt-6 border-t border-gray-200">
                        <form action="follow_handler.php" method="POST">
                            <input type="hidden" name="artist_id" value="<?php echo $artist['user_id']; ?>">
                            
                            <?php if (isset($_SESSION['user_id'])): ?>
                                <?php if ($is_following): ?>
                                    <button type="submit" name="unfollow" class="w-full bg-gray-200 text-gray-700 font-bold py-3 px-6 rounded-lg text-base hover:bg-gray-300 transition-colors">
                                        <i class="fas fa-check mr-2"></i> Following
                                    </button>
                                <?php else: ?>
                                    <button type="submit" name="follow" class="w-full bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark transition-colors text-base">
                                        <i class="fas fa-plus mr-2"></i> Follow Artist
                                    </button>
                                <?php endif; ?>
                            <?php else: ?>
                                <a href="login.php" class="w-full block text-center bg-gray-100 text-gray-600 font-bold py-3 px-6 rounded-lg hover:bg-gray-200 transition-colors text-base">
                                    Log in to follow
                                </a>
                            <?php endif; ?>
                        </form>
                    </div>

                </div>
            </div>
        </div>

        <!-- Placeholder for Artist's Paintings (Future) -->
        <div class="mt-16">
            <h2 class="text-3xl font-extrabold text-center text-ucf-charcoal mb-8">
                Artwork by <?php echo htmlspecialchars($artist['shop_name']); ?>
            </h2>
            <div class="text-center bg-white p-12 rounded-lg shadow-md">
                <i class="fas fa-palette fa-4x text-gray-300 mb-4"></i>
                <h3 class="text-2xl font-bold text-ucf-charcoal">Coming Soon</h3>
                <p class="text-gray-500 mt-2">This artist's gallery is currently empty. Please check back later!</p>
            </div>
        </div>

    </div>
</div>

<?php include 'footer.php'; ?>