<?php
/**
 * POST /api/v1/fcm_register.php
 *
 * Update the Firebase Cloud Messaging push token for the current device
 * (the device tied to this Bearer token). Called when FCM rotates the token.
 *
 * Body: { fcm_token }
 */

require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../lib/api.php';

api_require_method('POST');
$user = api_auth($pdo);

$body = api_body();
$fcm  = v_clean_string($body['fcm_token'] ?? '', 255);
if ($fcm === '') {
    api_error('missing_fcm_token', 422);
}

$pdo->prepare('UPDATE api_tokens SET fcm_token = ? WHERE id = ?')
    ->execute([$fcm, (int) $user['token_id']]);

api_ok();
