<?php
require_once 'includes/init.php';

if (!isAuthenticated()) {
    redirect(APP_URL . '/login.php');
}

$pageTitle = 'Blocked Users';
$activePage = 'settings';
$hideBottomNav = true;

$db = getDB();
$currentUserId = $_SESSION['user_id'];

// Handle unblock action
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'unblock') {
    $blockedId = sanitizeInput($_POST['blocked_id'] ?? '');
    
    if (!empty($blockedId) && is_numeric($blockedId)) {
        $stmt = $db->prepare("DELETE FROM blocked_users WHERE blocker_id = ? AND blocked_id = ?");
        $stmt->execute([$currentUserId, $blockedId]);
        
        header('Location: blocked-users.php?success=unblocked');
        exit;
    }
}

// Get blocked users
$stmt = $db->prepare("
    SELECT u.id, u.unique_id, u.name, u.profile_photo, u.last_seen, bu.blocked_at
    FROM blocked_users bu
    JOIN users u ON bu.blocked_id = u.id
    WHERE bu.blocker_id = ?
    ORDER BY bu.blocked_at DESC
");
$stmt->execute([$currentUserId]);
$blockedUsers = $stmt->fetchAll();

include 'includes/header.php';
?>

<div class="relative flex h-screen w-full flex-col overflow-hidden bg-background-light dark:bg-background-dark max-w-md mx-auto">
    <!-- Header -->
    <header class="flex items-center bg-background-light dark:bg-background-dark p-4 border-b border-slate-200 dark:border-slate-800 sticky top-0 z-10">
        <div class="flex items-center gap-3 flex-1">
            <a href="settings.php" class="text-slate-600 dark:text-slate-400 hover:text-primary transition-colors">
                <span class="material-symbols-outlined">arrow_back</span>
            </a>
            <h2 class="text-slate-900 dark:text-slate-100 text-xl font-bold">Blocked Users</h2>
        </div>
    </header>

    <main class="flex-1 overflow-y-auto no-scrollbar pb-24">
        <?php if (isset($_GET['success']) && $_GET['success'] === 'unblocked'): ?>
        <div class="m-4 bg-green-100 dark:bg-green-900/20 border border-green-200 dark:border-green-800 text-green-700 dark:text-green-400 px-4 py-3 rounded-xl text-sm">
            User unblocked successfully
        </div>
        <?php endif; ?>

        <?php if (empty($blockedUsers)): ?>
        <div class="flex flex-col items-center justify-center h-64 text-center p-8">
            <div class="size-16 rounded-full bg-slate-100 dark:bg-slate-800 flex items-center justify-center mb-4">
                <span class="material-symbols-outlined text-2xl text-slate-400">block</span>
            </div>
            <h3 class="text-slate-900 dark:text-slate-100 text-lg font-semibold mb-2">No Blocked Users</h3>
            <p class="text-slate-500 dark:text-slate-400 text-sm">You haven't blocked any users yet</p>
        </div>
        <?php else: ?>
        <div class="p-4 space-y-3">
            <?php foreach ($blockedUsers as $user): ?>
            <div class="bg-white dark:bg-slate-800 rounded-xl border border-slate-200 dark:border-slate-700 overflow-hidden">
                <div class="flex items-center gap-3 p-4">
                    <div class="relative shrink-0">
                        <?php if ($user['profile_photo']): ?>
                        <div class="bg-center bg-no-repeat aspect-square bg-cover rounded-full h-12 w-12 border-2 border-slate-200 dark:border-slate-600" style="background-image: url('uploads/images/<?php echo htmlspecialchars($user['profile_photo']); ?>')"></div>
                        <?php else: ?>
                        <div class="size-12 rounded-full bg-slate-100 dark:bg-slate-700 flex items-center justify-center border-2 border-slate-200 dark:border-slate-600">
                            <span class="material-symbols-outlined text-slate-500">person</span>
                        </div>
                        <?php endif; ?>
                    </div>
                    <div class="flex-1 min-w-0">
                        <h3 class="text-slate-900 dark:text-slate-100 font-semibold truncate"><?php echo htmlspecialchars($user['name']); ?></h3>
                        <p class="text-slate-500 dark:text-slate-400 text-sm">@<?php echo htmlspecialchars($user['unique_id']); ?></p>
                        <p class="text-slate-400 dark:text-slate-500 text-xs">Blocked on <?php echo date('M j, Y', strtotime($user['blocked_at'])); ?></p>
                    </div>
                    <form method="POST" action="" onsubmit="return confirm('Are you sure you want to unblock this user?')">
                        <input type="hidden" name="action" value="unblock">
                        <input type="hidden" name="blocked_id" value="<?php echo $user['id']; ?>">
                        <button type="submit" class="flex items-center gap-1 px-3 py-1.5 bg-green-100 dark:bg-green-900/20 text-green-600 dark:text-green-400 rounded-lg hover:bg-green-200 dark:hover:bg-green-800/30 transition-colors text-sm">
                            <span class="material-symbols-outlined text-sm">person_add</span>
                            Unblock
                        </button>
                    </form>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </main>
</div>

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