<?php
session_start();
require 'config.php';

// ✅ دالة shutdown لمعالجة الأخطاء القاتلة (Fatal Errors)
register_shutdown_function(function() {
    $error = error_get_last();
    if ($error && ($error['type'] === E_ERROR || $error['type'] === E_PARSE || $error['type'] === E_CORE_ERROR || $error['type'] === E_COMPILE_ERROR)) {
        if (ob_get_length()) ob_clean();
        http_response_code(500);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode(['status' => 'error', 'message' => 'حدث خطأ قاتل: ' . $error['message']], JSON_UNESCAPED_UNICODE);
        exit;
    }
});
require 'geofencing_service.php';

header('Content-Type: application/json; charset=UTF-8');

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['status' => 'error', 'message' => 'غير مسموح']);
    exit;
}

$data = json_decode(file_get_contents('php://input'), true);
$geofenceId = $data['id'] ?? null;

if (!$geofenceId) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => 'معرف المنطقة مطلوب']);
    exit;
}

$geofencingService = new GeofencingService($conn, $_SESSION['user_id']);
$result = $geofencingService->deleteGeofence($geofenceId);

echo json_encode([
    'status' => $result ? 'success' : 'error',
    'message' => $result ? 'تم الحذف بنجاح' : 'فشل الحذف'
], JSON_UNESCAPED_UNICODE);

