
<?php
// ═══════════════════════════════════════════════════════════════
// get_all_driver_fingerprints.php
// ───────────────────────────────────────────────────────────────
// ⚠️ يُستخدم مرة واحدة فقط ثم يُحذف من السيرفر
// ═══════════════════════════════════════════════════════════════

require_once __DIR__ . '/../get_connect.php';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: https://intaleqapp.com');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

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

// ── التحقق من admin_key ────────────────────────────────────────
$adminKey         = filterRequest('admin_key') ?? '';
$expectedAdminKey = getenv('MIGRATION_ADMIN_KEY');

if (empty($adminKey) || !hash_equals($expectedAdminKey, $adminKey)) {
    http_response_code(403);
    exit(json_encode(['error' => 'Forbidden']));
}

try {
    $stmt = $con->prepare('
        SELECT captain_id, fingerPrint
        FROM driverToken
        WHERE fingerPrint IS NOT NULL
          AND fingerPrint != ""
        ORDER BY captain_id
    ');
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'status' => 'success',
        'count'  => count($rows),
        'data'   => $rows,
    ]);

} catch (Exception $e) {
    error_log('❌ [get_all_driver_fingerprints] ' . $e->getMessage());
    http_response_code(500);
    echo json_encode(['error' => 'Server error']);
}
