<?php
// Build a clean import dataset: per item -> title, body text, materials links.
$dir = __DIR__;
$load = fn ($f) => json_decode(file_get_contents("$dir/$f"), true);
$all = array_merge($load('courses.json'), $load('lessons.json'), $load('topics.json'));

$DEMO = [825, 850, 874];
function s($v) { return is_array($v) ? ($v['rendered'] ?? $v['raw'] ?? json_encode($v)) : (string) $v; }
function clean($html) {
    $html = s($html);
    $html = preg_replace('/<!--.*?-->/s', '', $html);
    $html = preg_replace('#<(script|style)\b.*?</\1>#is', '', $html);
    $html = preg_replace('#<wistia-player[^>]*>.*?</wistia-player>#is', '', $html);
    $html = preg_replace('#<wistia-player[^>]*/?>#is', '', $html);
    $html = preg_replace('/\[[^\]]*\]/', '', $html);
    $t = trim(html_entity_decode(strip_tags($html), ENT_QUOTES));
    return trim(preg_replace("/[ \t]*\n{3,}/", "\n\n", $t));
}
function links($html) {
    $html = s($html);
    $out = [];
    if (preg_match_all('#<a\s[^>]*href=["\']([^"\']+)["\'][^>]*>(.*?)</a>#is', $html, $m, PREG_SET_ORDER)) {
        foreach ($m as $x) {
            $url = html_entity_decode($x[1], ENT_QUOTES);
            $text = trim(html_entity_decode(strip_tags($x[2]), ENT_QUOTES));
            if (stripos($url, 'http') === 0) $out[] = ['url' => $url, 'text' => $text];
        }
    }
    return $out;
}

$export = [];
$urls = [];
foreach ($all as $it) {
    $cid = $it['course'] ?? $it['id'];
    if (in_array($it['id'], $DEMO) || in_array($cid, $DEMO)) continue;
    $matLinks = links($it['materials'] ?? '');
    $bodyLinks = links($it['content']['raw'] ?? '');
    $merged = [];
    foreach (array_merge($matLinks, $bodyLinks) as $l) { $merged[$l['url']] = $l['text']; $urls[$l['url']] = true; }
    $export[$it['id']] = [
        'id' => $it['id'],
        'type' => $it['type'] ?? '?',
        'title' => trim(html_entity_decode(strip_tags(s($it['title'])), ENT_QUOTES)),
        'body' => clean($it['content']['raw'] ?? $it['content']['rendered'] ?? ''),
        'materials_text' => clean($it['materials'] ?? ''),
        'links' => array_map(fn ($u, $t) => ['url' => $u, 'text' => $t], array_keys($merged), array_values($merged)),
    ];
}

file_put_contents("$dir/import2.json", json_encode($export, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
file_put_contents("$dir/files.txt", implode("\n", array_keys($urls)));

echo "items: ".count($export)."  unique links: ".count($urls)."\n\n=== ALL LINKS ===\n";
foreach (array_keys($urls) as $u) echo "$u\n";
echo "\n=== items with body or materials ===\n";
foreach ($export as $e) {
    if (strlen($e['body']) || strlen($e['materials_text']) || count($e['links'])) {
        printf("#%-5d %-44s body=%-5d matText=%-4d links=%d\n", $e['id'], substr($e['title'],0,44), strlen($e['body']), strlen($e['materials_text']), count($e['links']));
    }
}
