#!/usr/bin/env python3
"""Task 14 migration: add matches.rules column (admin-written match rules).

Idempotent — safe to run multiple times.
"""
import os
import sys
import sqlite3

BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB = os.path.join(BASE, "data.sqlite")


def main():
    if not os.path.isfile(DB):
        print(f"[migrate_task14] DB not found: {DB}")
        sys.exit(1)
    con = sqlite3.connect(DB)
    cur = con.cursor()
    cols = [r[1] for r in cur.execute("PRAGMA table_info(matches)")]
    changed = False
    if "rules" not in cols:
        cur.execute("ALTER TABLE matches ADD COLUMN rules TEXT")
        print("[migrate_task14] added matches.rules TEXT")
        changed = True
    else:
        print("[migrate_task14] matches.rules already exists — skip")
    if changed:
        con.commit()
    con.close()
    print("[migrate_task14] done")


if __name__ == "__main__":
    main()
