<?php
// Smart field parser — copy paste ke DocumentExportService
// Ini logika smartField yang baru — di-copy ke method yg benar

private function smartField($section, ?string $label, string $value): void
{
    if (empty(trim($value))) return;
    $lines = explode("\n", trim($value));
    $lines = array_map('trim', $lines);
    $lines = array_filter($lines, fn($l) => $l !== '');
    $lines = array_values($lines);
    if (empty($lines)) return;

    // Split baris panjang (A. / B. / JUDUL: / SOAL/TUGAS:)
    if (count($lines) === 1 && strlen($lines[0]) > 200) {
        $s = $lines[0];
        $sp = preg_split('/(?=(?:A\.|B\.|C\.|D\.)\s+(?:PILIHAN|URAIAN|PRAKTIK|SOAL|ESAI))/i', $s);
        if (count($sp) > 1) $lines = array_map('trim', $sp);
        else {
            $sp2 = preg_split('/(?=(?:JUDUL|PETUNJUK|SOAL\/TUGAS|KESIMPULAN|RUBRIK|BENTUK ASESMEN|INSTRUMEN):)/i', $s);
            if (count($sp2) > 1) $lines = array_map('trim', $sp2);
        }
    }
    if ($label) $section->addText($label, ['bold' => true, 'size' => 10]);

    // 1. TAHAP
    if (preg_match('/^TAHAP\s+\d/i', $lines[0])) {
        foreach ($lines as $line) {
            if (preg_match('/^(TAHAP\s+\d+\s*[-–]\s*[^:]+):(.*)/i', $line, $m)) {
                $section->addText(trim($m[1]), ['bold' => true, 'size' => 10]);
                $section->addText(trim($m[2]), ['size' => 10], ['alignment' => Jc::BOTH]);
            } else $section->addText($line, ['size' => 10], ['alignment' => Jc::BOTH]);
        } $section->addTextBreak(1); return;
    }
    // 2. Rubrik
    if (preg_match('/^(Sangat Baik|Baik|Cukup|Kurang)\s*:/i', $lines[0])) {
        $t = $section->addTable(['borderSize' => 6, 'borderColor' => '222222', 'cellMargin' => 60]);
        $t->addRow();
        $t->addCell(2500, ['bgColor' => 'E8E8E8'])->addText('Level', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(7500, ['bgColor' => 'E8E8E8'])->addText('Deskripsi', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $colors = ['Sangat Baik' => 'DBEAFE', 'Baik' => 'D1FAE5', 'Cukup' => 'FEF3C7', 'Kurang' => 'FEE2E2'];
        foreach ($lines as $line) {
            $pos = strpos($line, ':'); if ($pos === false) continue;
            $lvl = trim(substr($line, 0, $pos));
            $desc = trim(substr($line, $pos + 1));
            $t->addRow();
            $t->addCell(2500, ['bgColor' => $colors[$lvl] ?? 'FFFFFF'])->addText($lvl, ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
            $t->addCell(7500)->addText($desc, ['size' => 9.5]);
        } $section->addTextBreak(1); return;
    }
    // 3. Numbered + → (asesmen diagnostik)
    if (preg_match('/^\d+\.\s/', $lines[0]) && strpos($lines[0], '→') !== false) {
        $t = $section->addTable(['borderSize' => 6, 'borderColor' => '222222', 'cellMargin' => 60]);
        $t->addRow();
        $t->addCell(400, ['bgColor' => 'E8E8E8'])->addText('No', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(4800, ['bgColor' => 'E8E8E8'])->addText('Pertanyaan', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(4800, ['bgColor' => 'E8E8E8'])->addText('Respons', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $no = 0;
        foreach ($lines as $line) {
            if (preg_match('/^\d+\.\s+(.+?)\s*→\s*(.+)/', $line, $m)) {
                $no++; $t->addRow();
                $t->addCell(400)->addText($no.'.', ['size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(4800)->addText(trim($m[1]), ['size' => 9.5]);
                $t->addCell(4800)->addText(trim($m[2]), ['size' => 9.5]);
            }
        } $section->addTextBreak(1); return;
    }
    // 4. Numbered + colon: "1. Model Utama: ..." atau "1. Konsep Esensial: ..."
    if (preg_match('/^\d+\.\s+\S+.*:.*\S/', $lines[0])) {
        $t = $section->addTable(['borderSize' => 6, 'borderColor' => '222222', 'cellMargin' => 60]);
        $t->addRow();
        $t->addCell(400, ['bgColor' => 'E8E8E8'])->addText('No', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(2500, ['bgColor' => 'E8E8E8'])->addText('Komponen', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(7100, ['bgColor' => 'E8E8E8'])->addText('Uraian', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $no = 0;
        foreach ($lines as $line) {
            if (preg_match('/^\d+\.\s+([^:]+):\s*(.+)/', $line, $m)) {
                $no++; $t->addRow();
                $t->addCell(400)->addText($no.'.', ['size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(2500, ['bgColor' => 'F5F5F5'])->addText(trim($m[1]), ['bold' => true, 'size' => 9.5]);
                $t->addCell(7100)->addText(trim($m[2]), ['size' => 9.5]);
            }
        } $section->addTextBreak(1); return;
    }
    // 5. Numbered biasa → tabel 2 kolom
    if (preg_match('/^\d+\.\s/', $lines[0])) {
        $t = $section->addTable(['borderSize' => 6, 'borderColor' => '222222', 'cellMargin' => 60]);
        $t->addRow();
        $t->addCell(400, ['bgColor' => 'E8E8E8'])->addText('No', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $t->addCell(9600, ['bgColor' => 'E8E8E8'])->addText('Uraian', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
        $no = 0;
        foreach ($lines as $line) {
            if (preg_match('/^\d+\.\s+(.+)/', $line, $m)) {
                $no++; $t->addRow();
                $t->addCell(400)->addText($no.'.', ['size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(9600)->addText(trim($m[1]), ['size' => 9.5]);
            }
        } $section->addTextBreak(1); return;
    }
    // 6. Tabel Kategori: Isi (2+ baris, semuanya punya :)
    if (count($lines) >= 2) {
        $hasColon = true;
        foreach ($lines as $l) { if (strpos($l, ':') === false) { $hasColon = false; break; } }
        $skip = preg_match('/^(Sangat Baik|Baik|Cukup|Kurang|TAHAP|\d+\.)/i', $lines[0]);
        if ($hasColon && !$skip) {
            $three = (substr_count($lines[0], ':') >= 2);
            $t = $section->addTable(['borderSize' => 6, 'borderColor' => '222222', 'cellMargin' => 60]);
            if ($three) {
                $t->addRow();
                $t->addCell(2500, ['bgColor' => 'E8E8E8'])->addText('Kategori', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(3500, ['bgColor' => 'E8E8E8'])->addText('Deskripsi', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(4000, ['bgColor' => 'E8E8E8'])->addText('Strategi', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
                foreach ($lines as $line) {
                    $parts = explode(':', $line, 3);
                    if (count($parts) >= 3) {
                        $t->addRow();
                        $t->addCell(2500, ['bgColor' => 'F5F5F5'])->addText(trim($parts[0]), ['bold' => true, 'size' => 9.5]);
                        $t->addCell(3500)->addText(trim($parts[1]), ['size' => 9.5]);
                        $t->addCell(4000)->addText(trim($parts[2]), ['size' => 9.5]);
                    }
                }
            } else {
                $t->addRow();
                $t->addCell(3000, ['bgColor' => 'E8E8E8'])->addText('Item', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
                $t->addCell(7000, ['bgColor' => 'E8E8E8'])->addText('Rincian', ['bold' => true, 'size' => 9.5], ['alignment' => Jc::CENTER]);
                foreach ($lines as $line) {
                    $pos = strpos($line, ':');
                    if ($pos !== false) {
                        $t->addRow();
                        $t->addCell(3000, ['bgColor' => 'F5F5F5'])->addText(trim(substr($line, 0, $pos)), ['bold' => true, 'size' => 9.5]);
                        $t->addCell(7000)->addText(trim(substr($line, $pos + 1)), ['size' => 9.5]);
                    }
                }
            }
            $section->addTextBreak(1); return;
        }
    }
    // 7. Sub-heading A./B./C.
    if (preg_match('/^[A-C]\.\s+(PILIHAN|URAIAN|PRAKTIK|SOAL|ESAI)/i', $lines[0])) {
        foreach ($lines as $line) {
            if (preg_match('/^[A-C]\.\s+/i', $line)) $section->addText($line, ['bold' => true, 'size' => 10]);
            else $section->addText($line, ['size' => 10]);
        } $section->addTextBreak(1); return;
    }
    // 8. Bullet
    if (str_starts_with($lines[0], '- ')) {
        foreach ($lines as $line) $section->addText($line, ['size' => 10], ['alignment' => Jc::BOTH]);
        $section->addTextBreak(1); return;
    }
    // 9. LKPD (JUDUL: / PETUNJUK: / SOAL: / KESIMPULAN:)
    if (preg_match('/^(JUDUL|PETUNJUK|SOAL|KESIMPULAN):/i', $lines[0])) {
        foreach ($lines as $line) {
            if (preg_match('/^(JUDUL|PETUNJUK|SOAL\/TUGAS|KESIMPULAN|RUBRIK):/i', $line))
                $section->addText($line, ['bold' => true, 'size' => 10]);
            else $section->addText($line, ['size' => 10], ['alignment' => Jc::BOTH]);
        } $section->addTextBreak(1); return;
    }
    // 10. Default
    foreach ($lines as $line) $section->addText($line, ['size' => 10], ['alignment' => Jc::BOTH]);
    $section->addTextBreak(1);
}
