<?php
// PHP session synchronizer endpoint for Firebase Auth

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$action = $_POST['action'] ?? '';

if ($action === 'login') {
    $uid = $_POST['uid'] ?? '';
    $email = $_POST['email'] ?? '';
    $token = $_POST['token'] ?? '';
    
    if ($uid !== '') {
        $_SESSION['fc_user'] = [
            'uid' => $uid,
            'email' => $email,
            'token' => $token
        ];
        echo json_encode(['status' => 'success', 'message' => 'Logged in']);
    } else {
        http_response_code(400);
        echo json_encode(['status' => 'error', 'message' => 'Missing UID']);
    }
} elseif ($action === 'logout') {
    $_SESSION = [];
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    session_destroy();
    echo json_encode(['status' => 'success', 'message' => 'Logged out']);
} else {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
}
