#!/opt/homebrew/bin/python3.11
"""Insert sentence chunks from transcribe.py output into dev.chunk table."""
import json, sys
import psycopg2

DB = "dbname=momentry user=accusys"
UUID = sys.argv[1] if len(sys.argv) > 1 else "23b1c872379d4ec06479e5ed39eef4c5"
ASR_PATH = f"/Users/accusys/momentry/output_dev/{UUID}.asr.json"
FPS = 23.976023976023978

with open(ASR_PATH) as f:
    asr = json.load(f)

segments = asr.get("segments", [])
print(f"Inserting {len(segments)} sentence chunks for {UUID}...")

conn = psycopg2.connect(DB)
cur = conn.cursor()

inserted = 0
for seg in segments:
    chunk_id = seg["chunk_id"]
    start_time = seg["start_time"]
    end_time = seg["end_time"]
    start_frame = int(start_time * FPS)
    end_frame = int(end_time * FPS)
    text = seg.get("text", "")
    speaker_change = seg.get("speaker_change", False)

    content = json.dumps({
        "source": "transcribe",
        "speaker_change": speaker_change,
        "pass1_index": seg.get("pass1_index", 0),
    })

    cur.execute("""
        INSERT INTO dev.chunk (file_uuid, chunk_id, chunk_type, start_time, end_time,
            start_frame, end_frame, fps, text_content, content, created_at)
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s::jsonb, NOW())
        ON CONFLICT (file_uuid, chunk_id) DO NOTHING
    """, (UUID, chunk_id, "sentence", start_time, end_time,
          start_frame, end_frame, FPS, text, content))
    inserted += 1

conn.commit()
cur.close()
conn.close()
print(f"Done: {inserted} chunks inserted")
