<?php
require_once __DIR__.'/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__.'/../../engine/Logger.php';
require_once __DIR__.'/../../engine/AccountingEngine.php';
if($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit;}
$ctx = Kernel::handle(['auth' => true]);
$u   = $ctx['user'];
if(empty($u['is_admin'])){http_response_code(403);die(json_encode(['error'=>'admin_required']));}
$b = json_decode(file_get_contents('php://input'),true)??[];
$date = preg_replace('/[^0-9\-]/','', $b['date']??date('Y-m-d'));
$result = AccountingEngine::runDailyReconciliation($date);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
