<?php
header('Content-Type: application/json');
require_once '../includes/db.php';

$data = json_decode(file_get_contents('php://input'), true);

// Debug logging
file_put_contents('fcm_log.txt', date('[Y-m-d H:i:s] ') . json_encode($data) . PHP_EOL, FILE_APPEND);

if (!isset($data['phone']) || !isset($data['token'])) {
    echo json_encode(['success' => false, 'error' => 'Missing phone or token']);
    exit;
}

try {
    $stmt = $pdo->prepare("INSERT INTO fcm_tokens (phone, token) VALUES (?, ?) ON DUPLICATE KEY UPDATE token = VALUES(token)");
    $stmt->execute([$data['phone'], $data['token']]);
    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'error' => 'Database error']);
}
?>
