<?php
require_once 'includes/EtimsIntegration.php';
include 'layouts/dbconn.php';

echo "<h2>Testing eTIMS Mock Mode</h2>";

$etims = new EtimsIntegration($conn);

// Check if mock mode is enabled
if (!$etims->isMockMode()) {
    echo "<p style='color: red;'>❌ Mock Mode is NOT enabled!</p>";
    echo "<p>Please enable it first.</p>";
    exit;
}

echo "<p style='color: green;'>✅ Mock Mode is ENABLED</p><hr>";

// Test 1: Register Items
echo "<h3>Test 1: Register Items</h3>";
$items = [
    [
        'item_name' => 'ENT Consultation',
        'item_code' => 'ENT-000001',
        'class_code' => '50101502',
        'price' => 3000.00,
        'tax_type' => 'B'
    ]
];

$result = $etims->registerItems($items);
echo "<pre>" . json_encode($result, JSON_PRETTY_PRINT) . "</pre>";

// Test 2: Submit Invoice
echo "<h3>Test 2: Submit Sales Invoice</h3>";
$invoice = [
    'invoice_number' => 5001,
    'trader_invoice_no' => 'INV-2025-001',
    'customer_name' => 'John Doe',
    'customer_pin' => 'A001234567X',
    'customer_mobile' => '254712345678',
    'customer_address' => 'Nairobi',
    'amount_paid' => 3240,
    'change_given' => 0,
    'items' => [
        [
            'item_code' => 'ENT-000001',
            'item_class_code' => '50101502',
            'item_name' => 'ENT Consultation',
            'quantity' => 1,
            'unit_price' => 3240,
            'tax_type' => 'B'
        ]
    ]
];

$result = $etims->submitInvoice($invoice);
echo "<pre>" . json_encode($result, JSON_PRETTY_PRINT) . "</pre>";

if ($result['success']) {
    echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px; margin-top: 20px;'>";
    echo "<h4 style='color: #155724;'>✅ Mock Mode is Working Perfectly!</h4>";
    echo "<p><strong>Receipt Signature:</strong> " . substr($result['data']['receiptSignature'], 0, 50) . "...</p>";
    echo "<p><strong>CU Serial:</strong> " . $result['data']['cuSerial'] . "</p>";
    echo "<p>You can now continue development without the sandbox.</p>";
    echo "</div>";
}

echo "<br><br>";
echo "<a href='test_etims_comprehensive.php' style='padding: 10px 20px; background: #4CAF50; color: white; text-decoration: none; border-radius: 4px;'>🔄 Run Full Test Suite</a>";
?>