<?php
require_once __DIR__ . '/../../connect.php';

// استقبال ID السجل
$id = filterRequest("id");

// التحقق من أن السجل يخص المستخدم الحالي أو هو أدمن
$checkSql = "SELECT driverID FROM captains_car WHERE id = :id LIMIT 1";
$checkStmt = $con->prepare($checkSql);
$checkStmt->bindParam(':id', $id, PDO::PARAM_INT);
$checkStmt->execute();
$record = $checkStmt->fetch(PDO::FETCH_ASSOC);

if (!$record) {
    jsonError("Record not found");
    exit;
}

if ($role !== 'admin' && $role !== 'super_admin' && (string)$user_id !== $record['driverID']) {
    jsonError("Unauthorized: You can only delete your own car registrations");
    exit;
}

// حذف السجل من جدول captains_car (أو CarRegistration لو هو الصحيح فعلاً)
$sql = "DELETE FROM captains_car WHERE id = :id";
$stmt = $con->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();

// التحقق من نجاح الحذف
if ($stmt->rowCount() > 0) {
    jsonSuccess(null, "Car registration deleted successfully");
} else {
    jsonError("Failed to delete car registration");
}
?>