<?php
/**
 * Contract HTML API — Returns the Local 48 contract as structured HTML
 * with section IDs for highlight/scroll targeting.
 */
require_once __DIR__ . '/../config/auth.php';
requireLogin();

$cacheFile = __DIR__ . '/../uploads/contract_html_cache.html';
if (file_exists($cacheFile)) {
    header('Content-Type: text/html; charset=utf-8');
    readfile($cacheFile);
    exit;
}

$contractFile = __DIR__ . '/../assets/data/l48-contract.txt';
if (!file_exists($contractFile)) {
    http_response_code(404);
    echo 'Contract not found';
    exit;
}

$text = file_get_contents($contractFile);
$lines = explode("\n", $text);

// Skip past the Table of Contents — find last line with dotted leaders (.......)
$bodyStart = 0;
for ($i = 0; $i < count($lines); $i++) {
    if (strpos($lines[$i], '......') !== false) {
        $bodyStart = $i + 1;
    }
}
// Now find the first real ARTICLE after the TOC
for ($i = $bodyStart; $i < count($lines); $i++) {
    if (preg_match('/^ARTICLE\s+\d+\s*[-–]/', $lines[$i])) {
        $bodyStart = $i;
        break;
    }
}

$html = '<div class="contract-body">';
$currentArticle = '';
$currentSection = '';

for ($i = $bodyStart; $i < count($lines); $i++) {
    $line = $lines[$i];
    $trimmed = trim($line);

    if ($trimmed === '') continue;

    // Page numbers (single number on a line)
    if (preg_match('/^\d{1,3}$/', $trimmed)) continue;

    // Article header
    if (preg_match('/^ARTICLE\s+(\d+)\s*[-–]\s*(.+)/i', $trimmed, $m)) {
        $artNum = (int)$m[1];
        $artTitle = trim($m[2]);
        $id = 'article-' . $artNum;
        $currentArticle = $artNum;
        $html .= '<div class="contract-article" id="' . $id . '" data-ref="Article ' . $artNum . '">';
        $html .= '<h2 class="contract-article-title"><span class="contract-ref-anchor">Article ' . $artNum . '</span> — ' . htmlspecialchars($artTitle) . '</h2>';
        continue;
    }

    // Section header
    if (preg_match('/^Section\s+(\d+)\.(\d+)\s*(.*)/i', $trimmed, $m)) {
        $secFull = $m[1] . '.' . $m[2];
        $secTitle = trim($m[3]);
        $id = 'section-' . str_replace('.', '-', $secFull);
        $currentSection = $secFull;
        $html .= '<div class="contract-section" id="' . $id . '" data-ref="Section ' . $secFull . '">';
        $html .= '<h3 class="contract-section-title"><span class="contract-ref-anchor">Section ' . htmlspecialchars($secFull) . '</span> ' . htmlspecialchars($secTitle) . '</h3>';
        continue;
    }

    // Step headers (Step One, Step Two, etc.)
    if (preg_match('/^Step\s+(One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|\d+)\s*[:\.]?/i', $trimmed)) {
        $html .= '<h4 class="contract-step">' . htmlspecialchars($trimmed) . '</h4>';
        continue;
    }

    // Regular paragraph
    $escaped = htmlspecialchars($trimmed);
    $html .= '<p class="contract-para">' . $escaped . '</p>';
}

$html .= '</div></div>';

@file_put_contents($cacheFile, $html);

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