<?php
session_start();
require 'config.php';
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);

$geofencingService = new GeofencingService($conn, $_SESSION['user_id']);

$result = $geofencingService->createGeofence(
    $data['name'],
    $data['type'],
    $data['latitude'],
    $data['longitude'],
    $data['radius'],
    $data['alert_on_enter'] ?? true,
    $data['alert_on_exit'] ?? true
);

if ($result) {
    echo json_encode(['status' => 'success', 'id' => $result]);
} else {
    echo json_encode(['status' => 'error', 'message' => 'فشل إنشاء المنطقة']);
}

