<?php
/**
 * Slice 8S PDF addendum · pricing PDF exports render without errors.
 *
 *  1) Controller pricingPdfAction dispatches by ?audience
 *  2) pricingPdfClient + pricingPdfInternal exist and use RsPdfReport via ExportHelper::pdfDownload
 *  3) View exposes Client PDF + Internal PDF buttons alongside the xlsx ones
 *  4) Warm palette + Roboto Condensed already available (RsPdfReport class present)
 *  5) Client PDF respects client_boq_visibility (LUMPSUM_ONLY / ITEMS_ONLY / ITEMS_AND_SECTIONS branches)
 *  6) Internal PDF landscape orientation (10-column shape)
 */
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__DIR__));
require_once ROOT . DS . 'config' . DS . 'config.php';

function h(string $t): void { echo "\n==== $t ====\n"; }
function ok(bool $c, string $m): void { echo ($c ? "  ✓ " : "  ✗ ") . $m . "\n"; }

/* ── 1. Controller surface ─────────────────────────────────────────── */
h('1. Controller surface');
$cBody = (string)file_get_contents(ROOT . DS . 'app' . DS . 'controllers' . DS . 'TendersController.php');
ok(strpos($cBody, 'function pricingPdfAction')      !== false, 'pricingPdfAction dispatcher defined');
ok(strpos($cBody, 'function pricingPdfClient')      !== false, 'pricingPdfClient defined');
ok(strpos($cBody, 'function pricingPdfInternal')    !== false, 'pricingPdfInternal defined');
ok(strpos($cBody, "audience === 'client'") !== false, 'dispatcher branches by audience');

/* ── 2. Uses ExportHelper::pdfDownload with orientation P for client, L for internal ─ */
h('2. Uses ExportHelper::pdfDownload with correct orientations');
ok(strpos($cBody, "ExportHelper::pdfDownload(\$fname . '.pdf', \$title, \$sub, \$columns, \$rows, \$totals, 'P')") !== false, 'client PDF is portrait (P)');
ok(strpos($cBody, "ExportHelper::pdfDownload(\$fname . '.pdf', \$title, \$sub, \$columns, \$rows, \$totals, 'L')") !== false, 'internal PDF is landscape (L)');

/* ── 3. Client PDF respects visibility modes ───────────────────────── */
h('3. Client PDF respects client_boq_visibility branches');
ok(strpos($cBody, "if (\$vis === 'LUMPSUM_ONLY')") !== false, 'LUMPSUM_ONLY branch present in PDF');
ok(strpos($cBody, "if (\$vis === 'ITEMS_AND_SECTIONS')") !== false, 'ITEMS_AND_SECTIONS branch present in PDF');

/* ── 4. Distribution math still applied in client PDF ──────────────── */
h('4. Client PDF applies pro-rata distribution');
// The word $distributedPool must appear twice — once each in xlsx and pdf client methods.
$hits = substr_count($cBody, '$distributedPool');
ok($hits >= 4, "distribution locals present in both xlsx and pdf client paths (found $hits references)");
ok(strpos($cBody, "\$clientLine = round(\$raw + \$share, 2)") !== false, 'clientLine computed identically in xlsx + pdf');

/* ── 5. Internal PDF includes overhead breakdown + margin summary ──── */
h('5. Internal PDF includes overhead breakdown + true vs client margin');
ok(strpos($cBody, '— Overhead pool (internal) —') !== false, 'overhead pool header line present');
ok(strpos($cBody, 'True margin % (post-oh)')      !== false, 'true margin summary present');
ok(strpos($cBody, 'Client-facing margin %')       !== false, 'client margin summary present');

/* ── 6. View surface: buttons ──────────────────────────────────────── */
h('6. View surface');
$vBody = (string)file_get_contents(ROOT . DS . 'app' . DS . 'views' . DS . 'Tenders' . DS . 'view.php');
// Slice 8U: buttons now call previewPricingPdf(audience) which builds the URL in the Vue root.
ok(strpos($vBody, "previewPricingPdf('client')")   !== false, 'client PDF button wired (iframe preview)');
ok(strpos($vBody, "previewPricingPdf('internal')") !== false, 'internal PDF button wired (iframe preview)');
ok(strpos($vBody, "'Tenders/pricingPdf/'") !== false, 'previewPricingPdf method builds pricingPdf URL');
ok(strpos($vBody, 'fa-file-pdf') !== false, 'PDF icon present');

/* ── 7. RsPdfReport class present + Roboto Condensed fonts ─────────── */
h('7. RsPdfReport + fonts');
$pdfClassPath = ROOT . DS . 'app' . DS . 'models' . DS . 'RsPdfReport.php';
ok(file_exists($pdfClassPath), 'RsPdfReport.php present');
$pdfBody = (string)file_get_contents($pdfClassPath);
ok(strpos($pdfBody, 'Roboto_Condensed-Regular') !== false, 'Roboto Condensed Regular referenced');
ok(strpos($pdfBody, 'Roboto_Condensed-Bold')    !== false, 'Roboto Condensed Bold referenced');
$fontRegular = ROOT . DS . 'fonts' . DS . 'fpdf' . DS . 'Roboto_Condensed-Regular.php';
$fontBold    = ROOT . DS . 'fonts' . DS . 'fpdf' . DS . 'Roboto_Condensed-Bold.php';
ok(file_exists($fontRegular), 'Roboto_Condensed-Regular.php font file present');
ok(file_exists($fontBold),    'Roboto_Condensed-Bold.php font file present');

/* ── 8. Bid amount UI restructure surface ──────────────────────────── */
h('8. Bid amount UI restructure surface');
ok(strpos($vBody, 'id="pricing_tab_panel"') !== false, 'pricing_tab_panel present');
ok(strpos($vBody, 'id="pricing_bid_preview"') !== false, 'pricing_bid_preview readonly input present');
ok(strpos($vBody, 'id="btn_pricing_sync"') !== false, 'resync button present');
ok(strpos($vBody, 'data-pv-sum-price') !== false, 'sum_price seeded from server');
ok(strpos($vBody, 'syncPricingBid') !== false, 'syncPricingBid JS defined');
ok(strpos($vBody, "type=\"hidden\" id=\"sub_amount\"") !== false, '#sub_amount is now hidden (POST payload only)');

echo "\n=== END ===\n";
