from pathlib import Path
import re, json
base=Path('/home/agent/outputs/moenchaltorf_fachplaner_jourfix_2_20260707')
text=(base/'source/teams_transcript.txt').read_text(encoding='utf-8')
# Pattern: Speaker   m:ss or h:mm:ss
pat=re.compile(r'^(?P<speaker>.+?)\s{2,}(?P<time>(?:\d+:)?\d+:\d{2})$')
entries=[]
cur=None

def sec(t):
    parts=[int(x) for x in t.split(':')]
    if len(parts)==2: return parts[0]*60+parts[1]
    return parts[0]*3600+parts[1]*60+parts[2]
for line in text.splitlines():
    line=line.strip('\ufeff').rstrip()
    m=pat.match(line.strip())
    if m:
        if cur and cur['text'].strip(): entries.append(cur)
        cur={'speaker':m.group('speaker').strip(), 'time':m.group('time'), 'start':sec(m.group('time')), 'text':''}
    else:
        if cur is not None:
            if line.strip(): cur['text'] += (' ' if cur['text'] else '') + line.strip()
if cur and cur['text'].strip(): entries.append(cur)
# remove pure filler short entries but keep raw too
(base/'transcription').mkdir(exist_ok=True)
(base/'transcription/teams_entries.json').write_text(json.dumps(entries, ensure_ascii=False, indent=2), encoding='utf-8')
with (base/'transcription/teams_entries_compact.txt').open('w', encoding='utf-8') as f:
    for e in entries:
        f.write(f"[{e['time']}] {e['speaker']}: {e['text']}\n")
print('entries', len(entries), 'speakers', sorted(set(e['speaker'] for e in entries)))
