#!/usr/bin/env python3
"""Build the client-side search index for the Easy Chat tutorial library.

Reads  <lessons>/index.json  and every  <lessons>/<slug>/lesson.json
Writes <lessons>/search-index.json  (compact, UTF-8, raw text: the page
normalizes Arabic itself so highlights can map back to the original words).

Format:
  {"v":1,"built":"<iso>","n":<count>,
   "l":[[slug, title_ar, title_en, module, "kw1|kw2|...", [[label, caption], ...]], ...]}
Slide order is preserved, so a slide's position in the list is its index
(0-based) -- the page links to it as  #<slug>/<index+1>.

Re-run after adding or editing lessons:
  python3 /home/moon/tour-tools/build-search-index.py
"""
import argparse
import json
import os
import sys
from datetime import datetime, timezone

DEFAULT_LESSONS = '/home/easychat/public_html/help/lessons'


def read_json(path):
    with open(path, encoding='utf-8') as fh:
        return json.load(fh)


def clean(text):
    return ' '.join(str(text or '').split())


def lesson_slides(lesson_dir):
    """Return [[label, caption], ...] for one lesson, or None if unreadable."""
    data = None
    for name in ('lesson.json', 'slides.json'):
        path = os.path.join(lesson_dir, name)
        if os.path.exists(path):
            data = read_json(path)
            break
    if data is None:
        return None
    narration = {}
    scenes_path = os.path.join(lesson_dir, 'scenes.json')
    if os.path.exists(scenes_path):
        try:
            for scene in read_json(scenes_path).get('scenes', []):
                narration[scene.get('key')] = scene.get('narration', '')
        except (ValueError, OSError) as err:
            print(f'  warn: {scenes_path}: {err}', file=sys.stderr)
    return [[clean(s.get('label')), clean(s.get('caption') or narration.get(s.get('key')))]
            for s in data.get('slides', [])]


def build(lessons_root):
    registry = read_json(os.path.join(lessons_root, 'index.json'))
    rows, missing = [], []
    for entry in registry.get('lessons', []):
        slug = entry.get('slug')
        if not slug or not entry.get('title_ar'):
            continue
        slides = None
        if entry.get('status', 'ready') == 'ready':
            try:
                slides = lesson_slides(os.path.join(lessons_root, slug))
            except (ValueError, OSError) as err:
                print(f'  warn: {slug}: {err}', file=sys.stderr)
        if slides is None:
            missing.append(slug)
            slides = []
        keywords = entry.get('keywords') or []
        if isinstance(keywords, str):
            keywords = [keywords]
        rows.append([slug, clean(entry.get('title_ar')), clean(entry.get('title_en')),
                     clean(entry.get('module')), '|'.join(clean(k) for k in keywords if k), slides])
    out = {'v': 1, 'built': datetime.now(timezone.utc).isoformat(timespec='seconds'),
           'n': len(rows), 'l': rows}
    return out, missing


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument('--lessons', default=DEFAULT_LESSONS, help='lessons directory (default: %(default)s)')
    args = ap.parse_args()
    out, missing = build(args.lessons)
    target = os.path.join(args.lessons, 'search-index.json')
    tmp = target + '.tmp'
    with open(tmp, 'w', encoding='utf-8') as fh:
        json.dump(out, fh, ensure_ascii=False, separators=(',', ':'))
    os.replace(tmp, target)  # atomic: the page never sees a half-written file
    slides = sum(len(r[5]) for r in out['l'])
    print(f'wrote {target}: {out["n"]} lessons, {slides} steps, {os.path.getsize(target)} bytes')
    if missing:
        print('no slides for: ' + ', '.join(missing))


if __name__ == '__main__':
    main()
