"""Idempotente Migration: Quellen-Typ pdf_document + EN-Spalten in articles.

Beim Live-Promote anwenden:
    python3 scripts/migrate_pdf_source.py /home/claude-dev/osint-data/osint.db
"""
import sqlite3
import sys


def add_col(db, table, col_def):
    name = col_def.split()[0]
    cols = {r[1] for r in db.execute(f"PRAGMA table_info({table})").fetchall()}
    if name in cols:
        return False
    db.execute(f"ALTER TABLE {table} ADD COLUMN {col_def}")
    return True


def main(path):
    with sqlite3.connect(path) as db:
        for col in ("pdf_path TEXT", "pdf_sha256 TEXT", "processed_at TIMESTAMP"):
            print(f"sources.{col.split()[0]}:", "added" if add_col(db, "sources", col) else "exists")
        for col in ("headline_en TEXT", "content_en TEXT"):
            print(f"articles.{col.split()[0]}:", "added" if add_col(db, "articles", col) else "exists")
        db.execute("CREATE INDEX IF NOT EXISTS idx_sources_pdf_sha256 ON sources(pdf_sha256)")
        db.commit()
    print("DONE")


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: migrate_pdf_source.py /path/to/osint.db")
        sys.exit(1)
    main(sys.argv[1])
