<?php
declare(strict_types=1);

require_once __DIR__ . '/../system/bootstrap.php';
require_once __DIR__ . '/../system/auth.php';
require_once __DIR__ . '/../system/audit.php';

header('Content-Type: application/json');

try {
    $user = require_auth($pdo);

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

    audit_log(
        $pdo,
        'audit_export',
        $user['id'],
        $user['company_id'] ?? null,
        [
            'filters' => [
                'search' => $input['search'] ?? null,
                'action' => $input['action'] ?? null,
                'browser' => $input['browser'] ?? null,
                'operating_system' => $input['operating_system'] ?? null,
                'date_from' => $input['date_from'] ?? null,
                'date_to' => $input['date_to'] ?? null,
            ],
            'export_format' => 'csv'
        ]
    );

    echo json_encode(['status' => 'ok']);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error']);
}
