<?php
// File: /admin/manage_users.php
include 'header.php'; // Includes security check and DB connection

// --- FETCH ALL USERS FROM THE DATABASE ---
$users = [];
// We join with the profiles tables to get more detailed information if available
$sql = "SELECT 
            u.id, 
            u.username, 
            u.email, 
            u.role, 
            u.created_at,
            ap.shop_name,
            ip.institution_name
        FROM users u
        LEFT JOIN artist_profiles ap ON u.id = ap.user_id AND u.role = 'artist'
        LEFT JOIN institution_profiles ip ON u.id = ip.user_id AND u.role = 'institution'
        ORDER BY u.created_at DESC";

$result = $conn->query($sql);
if ($result) {
    $users = $result->fetch_all(MYSQLI_ASSOC);
} else {
    // Handle potential SQL error
    echo "Error fetching users: " . $conn->error;
}
?>

<div class="p-6 md:p-8">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-6">Manage Users</h1>

    <div class="bg-white p-6 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">All Registered Users</h2>
        
        <!-- We can add a search bar here later -->

        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Username</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Role</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Registered On</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (!empty($users)): ?>
                        <?php foreach ($users as $user): ?>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">#<?php echo $user['id']; ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                    <?php echo htmlspecialchars($user['username']); ?>
                                    <?php if ($user['role'] === 'artist'): ?>
                                        <span class="block text-xs text-gray-500"><?php echo htmlspecialchars($user['shop_name']); ?></span>
                                    <?php elseif ($user['role'] === 'institution'): ?>
                                        <span class="block text-xs text-gray-500"><?php echo htmlspecialchars($user['institution_name']); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars($user['email']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                                        <?php 
                                            switch($user['role']) {
                                                case 'admin': echo 'bg-red-100 text-red-800'; break;
                                                case 'artist': echo 'bg-green-100 text-green-800'; break;
                                                case 'institution': echo 'bg-blue-100 text-blue-800'; break;
                                                default: echo 'bg-gray-100 text-gray-800';
                                            }
                                        ?>">
                                        <?php echo ucfirst($user['role']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                    <a href="#" class="text-indigo-600 hover:text-indigo-900">View</a>
                                    <!-- We will build the edit/delete functionality later -->
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">No users have registered yet.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
