"""
DB 스키마 마이그레이션.
- transcription_records: LLM 메타데이터 컬럼 추가 (title, summary, keywords, urgency)
- transcription_segments: speaker 컬럼 추가 (VARCHAR(10), 기본값 '불명')
"""
from sqlalchemy import create_engine, text, inspect

e = create_engine("sqlite:///whisper.db")
insp = inspect(e)

# ── 1. transcription_records 테이블 ───────────────────────────────────────────
existing_rec = [c["name"] for c in insp.get_columns("transcription_records")]
print("[ transcription_records ] 현재 컬럼:", existing_rec)

record_cols = [
    ("title",    "VARCHAR(256)"),
    ("summary",  "TEXT"),
    ("keywords", "VARCHAR(512)"),
    ("urgency",  "VARCHAR(16)"),
]

with e.connect() as conn:
    for col, typ in record_cols:
        if col not in existing_rec:
            conn.execute(text(f"ALTER TABLE transcription_records ADD COLUMN {col} {typ}"))
            print(f"  ✅ 추가됨: {col}")
        else:
            print(f"  ⏭️  이미 존재: {col}")
    conn.commit()

# ── 2. transcription_segments 테이블 ─────────────────────────────────────────
existing_seg = [c["name"] for c in insp.get_columns("transcription_segments")]
print("\n[ transcription_segments ] 현재 컬럼:", existing_seg)

segment_cols = [
    ("speaker",     "VARCHAR(10) DEFAULT '불명'"),
    ("is_reviewed", "BOOLEAN DEFAULT 0 NOT NULL"),  # False=0, 수동 검토 완료 여부
    ("audio_path",  "VARCHAR(512)"), # [Chapter 8.0] Opus 압축본 오디오 경로
]

with e.connect() as conn:
    for col, typ in segment_cols:
        if col not in existing_seg:
            conn.execute(text(f"ALTER TABLE transcription_segments ADD COLUMN {col} {typ}"))
            print(f"  ✅ 추가됨: {col}")
        else:
            print(f"  ⏭️  이미 존재: {col}")
    conn.commit()

print("\n마이그레이션 완료")

