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

if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    exit;
}

$deviceId = $_GET['device_id'] ?? null;
$startDate = $_GET['start_date'] ?? date('Y-m-d') . ' 00:00:00';
$endDate = $_GET['end_date'] ?? date('Y-m-d') . ' 23:59:59';
$format = $_GET['format'] ?? 'csv';

if (!$deviceId) {
    die('device_id مطلوب');
}

$exportService = new ExportService($conn, $_SESSION['user_id']);

if ($format === 'csv') {
    $exportService->exportToCSV($deviceId, $startDate, $endDate);
} else {
    die('تنسيق غير مدعوم');
}

