<?php
include 'db.php'; // Ensure database connection

header('Content-Type: application/json');

// Enable error reporting for debugging (Remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if request method is GET and user_id is provided
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['user_id'])) {
    $user_id = intval($_GET['user_id']);

    if ($user_id > 0) {
        // Prepare and execute query
        $stmt = $pdo->prepare("SELECT image FROM users WHERE id = ?");
        $stmt->execute([$user_id]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user && !empty($user['image'])) {
            echo json_encode(["status" => "success", "image_url" => $user['image']]);
        } else {
            echo json_encode(["status" => "error", "message" => "No image found for this user"]);
        }
    } else {
        echo json_encode(["status" => "error", "message" => "Invalid user ID"]);
    }
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request method or missing user ID"]);
}
?>
