<?php
// Extract lesson/topic/course materials + body text + every downloadable file URL.
$dir = __DIR__;
$load = fn ($f) => json_decode(file_get_contents("$dir/$f"), true);
$courses = $load('courses.json');
$lessons = $load('lessons.json');
$topics  = $load('topics.json');

$DEMO = [825, 850, 874];           // LearnDash baking demo courses to skip
$REAL_COURSES = [10, 1986, 1988, 2039, 1984, 1991];

$txt = fn ($v) => trim(html_entity_decode(strip_tags(is_array($v) ? ($v['rendered'] ?? $v['raw'] ?? '') : (string) $v), ENT_QUOTES));

$files = [];
function grabFiles($html, array &$bag): array {
    if (is_array($html)) $html = json_encode($html);
    $html = (string) $html;
    $hits = [];
    if (preg_match_all('#https?://[^\s"\'<>]+\.(?:pdf|docx?|xlsx?|pptx?|zip|csv|key|pages)#i', $html, $m)) {
        foreach ($m[0] as $u) { $hits[] = $u; $bag[$u] = true; }
    }
    return array_values(array_unique($hits));
}

// clean WP body to readable text (strip blocks, shortcodes, scripts, wistia)
function cleanBody($html): string {
    if (is_array($html)) $html = json_encode($html);
    $html = (string) $html;
    $html = preg_replace('/<!--.*?-->/s', '', $html);                 // wp block comments
    $html = preg_replace('#<script\b.*?</script>#is', '', $html);     // scripts (wistia)
    $html = preg_replace('#<style\b.*?</style>#is', '', $html);
    $html = preg_replace('#<wistia-player[^>]*>.*?</wistia-player>#is', '', $html);
    $html = preg_replace('#<wistia-player[^>]*/?>#is', '', $html);
    $html = preg_replace('/\[[^\]]*\]/', '', $html);                  // shortcodes
    $t = trim(html_entity_decode(strip_tags($html), ENT_QUOTES));
    $t = preg_replace("/\n{3,}/", "\n\n", $t);
    return trim($t);
}

$report = function ($label, $items, $skipCourses = []) use ($txt, &$files) {
    echo "\n==================== ".strtoupper($label)." ====================\n";
    foreach ($items as $it) {
        $cid = $it['course'] ?? ($it['id'] ?? 0);
        if ($label === 'courses' && in_array($it['id'], [825,850,874])) continue;
        if ($label !== 'courses' && in_array(($it['course'] ?? 0), [825,850,874])) continue;
        $mat = $it['materials'] ?? '';
        $mat = is_array($mat) ? json_encode($mat) : (string) $mat;
        $body = cleanBody($it['content']['raw'] ?? $it['content']['rendered'] ?? '');
        $matFiles = grabFiles($mat, $files);
        $bodyFiles = grabFiles($it['content']['raw'] ?? '', $files);
        $title = substr($txt($it['title']), 0, 46);
        printf("#%-5d %-46s bodyChars=%-5d matFiles=%d bodyFiles=%d\n", $it['id'], $title, strlen($body), count($matFiles), count($bodyFiles));
        foreach (array_unique(array_merge($matFiles, $bodyFiles)) as $f) echo "        FILE: $f\n";
        if (strlen($mat) > 0 && !count($matFiles)) echo "        materials(no-file): ".substr($txt($mat),0,120)."\n";
    }
};

$report('courses', $courses);
$report('lessons', $lessons);
$report('topics', $topics);

echo "\n==================== ALL UNIQUE FILES (".count($files).") ====================\n";
foreach (array_keys($files) as $u) echo "$u\n";
file_put_contents("$dir/files.txt", implode("\n", array_keys($files)));
