<?php
require_once __DIR__ . '/api/vendor/autoload.php';
require_once __DIR__ . '/api/config/database.php';
require_once __DIR__ . '/api/helpers/Response.php';
require_once __DIR__ . '/api/helpers/Validator.php';
require_once __DIR__ . '/api/helpers/UUID.php';
require_once __DIR__ . '/api/helpers/AuditLogger.php';
require_once __DIR__ . '/api/services/AccountingService.php';
require_once __DIR__ . '/api/controllers/ItemController.php';

try {
    $db = Database::connect();
    
    // Check items
    $stmt = $db->query("SELECT id, name FROM items LIMIT 5");
    $items = $stmt->fetchAll();
    echo "Items: \n";
    print_r($items);

    if (count($items) > 0) {
        $itemId = $items[0]['id'];
        echo "\nTesting Advanced Ledger for Item ID: $itemId\n";
        
        // mock user
        $user = (object)['id' => 1];
        $ctrl = new ItemController($db, $user);
        
        // Output buffering to catch the JSON response
        ob_start();
        $ctrl->advancedLedger($itemId, ['from_date' => '2026-01-01', 'to_date' => '2026-12-31']);
        $json = ob_get_clean();
        
        echo "\nResponse:\n";
        echo $json;
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
