<?php
require_once 'includes/db.php';
$db = getDB();

echo "=== SYSTEM VERIFICATION ===\n\n";

// Check if tables exist
echo "--- Database Tables ---\n";
$tables = ['purchases', 'purchase_items', 'invoices', 'products', 'customers', 'users'];
foreach ($tables as $table) {
    try {
        $result = $db->query("SHOW TABLES LIKE '$table'");
        if ($result->rowCount() > 0) {
            echo "✓ Table '$table' exists\n";
        } else {
            echo "✗ Table '$table' MISSING\n";
        }
    } catch (Exception $e) {
        echo "✗ Error checking '$table': " . $e->getMessage() . "\n";
    }
}

echo "\n--- Functions ---\n";
require_once 'includes/functions.php';

// Test invoice number generator
try {
    $inv_num = generateInvoiceNumber();
    echo "✓ Invoice number generator works: $inv_num\n";
} catch (Exception $e) {
    echo "✗ Invoice generator error: " . $e->getMessage() . "\n";
}

// Test purchase number generator
try {
    $pur_num = generatePurchaseNumber();
    echo "✓ Purchase number generator works: $pur_num\n";
} catch (Exception $e) {
    echo "✗ Purchase generator error: " . $e->getMessage() . "\n";
}

echo "\n--- Database Content ---\n";

// Check products count
try {
    $result = $db->query('SELECT COUNT(*) as cnt FROM products');
    $row = $result->fetch();
    echo "✓ Products in database: " . $row['cnt'] . "\n";
} catch (Exception $e) {
    echo "✗ Error counting products: " . $e->getMessage() . "\n";
}

// Check customers count
try {
    $result = $db->query('SELECT COUNT(*) as cnt FROM customers');
    $row = $result->fetch();
    echo "✓ Customers in database: " . $row['cnt'] . "\n";
} catch (Exception $e) {
    echo "✗ Error counting customers: " . $e->getMessage() . "\n";
}

// Check invoices count
try {
    $result = $db->query('SELECT COUNT(*) as cnt FROM invoices');
    $row = $result->fetch();
    echo "✓ Invoices in database: " . $row['cnt'] . "\n";
} catch (Exception $e) {
    echo "✗ Error counting invoices: " . $e->getMessage() . "\n";
}

// Check purchases count
try {
    $result = $db->query('SELECT COUNT(*) as cnt FROM purchases');
    $row = $result->fetch();
    echo "✓ Purchases in database: " . $row['cnt'] . "\n";
} catch (Exception $e) {
    echo "✗ Error counting purchases: " . $e->getMessage() . "\n";
}

echo "\n--- Dependencies ---\n";

// Check if PDF library is loaded
if (file_exists('vendor/autoload.php')) {
    echo "✓ DOMPDF library installed\n";
    try {
        require_once 'vendor/autoload.php';
        if (class_exists('Dompdf\Dompdf')) {
            echo "✓ DOMPDF class available\n";
        }
    } catch (Exception $e) {
        echo "⚠ DOMPDF load error: " . $e->getMessage() . "\n";
    }
} else {
    echo "✗ DOMPDF library NOT found\n";
}

// Check if files exist
echo "\n--- New Files ---\n";
$newFiles = [
    'pages/purchase_add.php',
    'pages/purchases.php',
    'pages/purchase_view.php',
    'pages/pdf_invoice.php'
];

foreach ($newFiles as $file) {
    if (file_exists($file)) {
        $size = filesize($file);
        echo "✓ $file (" . number_format($size) . " bytes)\n";
    } else {
        echo "✗ $file NOT FOUND\n";
    }
}

echo "\n=== VERIFICATION COMPLETE ===\n";
?>