<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
header('Access-Control-Max-Age: 86400');

// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

require_once '../config.php';

try {
    // Get user_id parameter to identify which user's logo to show
    $userId = isset($_GET['user_id']) ? intval($_GET['user_id']) : null;
    
    if (!$userId) {
        // No user ID provided, return no logo
        echo json_encode(['success' => true, 'logo_url' => null]);
        exit;
    }
    
    // Get the specific user's logo
    $stmt = $conn->prepare("SELECT logo_url FROM users WHERE id = ? AND logo_url IS NOT NULL AND logo_url != ''");
    if (!$stmt) {
        throw new Exception("Database prepare error: " . $conn->error);
    }
    
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo json_encode(['success' => true, 'logo_url' => $row['logo_url']]);
    } else {
        echo json_encode(['success' => true, 'logo_url' => null]);
    }
    
} catch (Exception $e) {
    error_log("Get user logo error: " . $e->getMessage());
    echo json_encode(['success' => false, 'error' => 'Failed to get logo']);
}
?>