<?php

declare(strict_types=1);

header('Content-Type: text/html; charset=utf-8');

$root = __DIR__;
$checks = [
    $root . '/vendor/autoload.php' => 'Main vendor autoload',
    $root . '/vendor/setasign/fpdf/fpdf.php' => 'FPDF library',
    $root . '/vendor/setasign/fpdi/src/Fpdi.php' => 'FPDI library',
    $root . '/invoice/vendor/autoload.php' => 'invoice/vendor autoload (wrong folder)',
    $root . '/invoice/vendor/setasign/fpdf/fpdf.php' => 'invoice/vendor FPDF',
];

$foundVendor = null;
foreach ([$root . '/vendor', $root . '/invoice/vendor'] as $dir) {
    if (is_file($dir . '/setasign/fpdf/fpdf.php')) {
        $foundVendor = $dir;
        break;
    }
}

$loadOk = false;
$loadError = '';
$pdfTestOk = false;
$pdfTestSize = 0;
try {
    require_once $root . '/includes/vendor_loader.php';
    karvi_load_vendor(true);
    $loadOk = class_exists('FPDF', false);

    if ($loadOk) {
        require_once $root . '/includes/bootstrap.php';
        require_once $root . '/includes/helpers.php';
        require_once $root . '/includes/invoice_contract.php';
        require_once $root . '/includes/invoice_pdf_generator.php';

        $testFile = $root . '/uploads/contracts/vendor_check_test.pdf';
        $contract = [
            'token' => 'vendorcheck000000000000000001',
            'id' => 1,
            'invoice_mode' => 'pos',
            'contract_line' => 'Karvi POS',
            'contract_date' => date('Y-m-d'),
            'delivery_date' => date('Y-m-d', time() + (25 * 86400)),
            'customer_details' => "Test GmbH\nMusterstr. 1\n60311 Frankfurt",
            'installment_months' => 12,
            'line_items' => [[
                'description' => 'Karvi POS Test',
                'quantity' => 1,
                'unit_price_net' => 100,
                'total_net' => 100,
            ]],
            'optional_printer' => 0,
            'discount' => 0,
            'totals' => invoice_calculate_totals([['total_net' => 100]], 0, false, ['unit_price_net' => 0]),
        ];
        generate_invoice_contract_pdf($contract, $testFile);
        if (is_file($testFile) && filesize($testFile) >= 512) {
            $pdfTestOk = true;
            $pdfTestSize = (int)filesize($testFile);
        }
    }
} catch (Throwable $e) {
    $loadError = $e->getMessage();
}

?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Vendor Check</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 760px; margin: 2rem auto; padding: 0 1rem; }
        .ok { color: green; } .bad { color: red; font-weight: bold; }
        code { background: #eee; padding: 2px 5px; }
        .box { background: #f8f8f8; border: 1px solid #ddd; padding: 1rem; border-radius: 8px; margin-top: 1rem; }
    </style>
</head>
<body>
    <h1>Vendor Check</h1>
    <p>Server folder: <code><?= htmlspecialchars($root, ENT_QUOTES, 'UTF-8') ?></code></p>

    <table border="1" cellpadding="8" cellspacing="0" width="100%">
        <tr><th>File</th><th>Status</th></tr>
        <?php foreach ($checks as $file => $label): ?>
        <tr>
            <td><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></td>
            <td class="<?= is_file($file) ? 'ok' : 'bad' ?>"><?= is_file($file) ? 'FOUND' : 'MISSING' ?></td>
        </tr>
        <?php endforeach; ?>
    </table>

    <p>Resolved vendor folder:
        <code><?= htmlspecialchars($foundVendor ?? 'none', ENT_QUOTES, 'UTF-8') ?></code>
    </p>
    <p>Autoload test:
        <?php if ($loadOk): ?>
            <span class="ok">OK — FPDF loaded</span>
        <?php else: ?>
            <span class="bad">FAIL — <?= htmlspecialchars($loadError, ENT_QUOTES, 'UTF-8') ?></span>
        <?php endif; ?>
    </p>
    <p>PDF generation test:
        <?php if ($pdfTestOk): ?>
            <span class="ok">OK — <?= (int)$pdfTestSize ?> bytes</span>
        <?php else: ?>
            <span class="bad">FAIL</span>
        <?php endif; ?>
    </p>

    <div class="box">
        <strong>Quick fix (FTP):</strong>
        <ol>
            <li>Zip se poora <code>vendor</code> folder upload karein</li>
            <li>Target: <code>/karvi-pro/vendor/</code></li>
            <li>Check file exists: <code>/karvi-pro/vendor/setasign/fpdf/fpdf.php</code></li>
            <li>Agar app <code>/karvi-pro/invoice/</code> mein hai to wahan bhi <code>vendor/</code> upload karein</li>
        </ol>
    </div>
</body>
</html>
