#!/usr/bin/env python3
"""Task 13 migration: add matches.reg_open_until_start (idempotent)."""
import sqlite3
import os
import sys

BASE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
DB = os.path.join(BASE, "data.sqlite")
if not os.path.isfile(DB):
    # fallback: search for the db file
    for root, _dirs, files in os.walk(BASE):
        for f in files:
            if f.endswith(".db"):
                DB = os.path.join(root, f)
                break
print("DB:", DB)
con = sqlite3.connect(DB)
cur = con.cursor()
cols = [r[1] for r in cur.execute("PRAGMA table_info(matches)").fetchall()]
if "reg_open_until_start" not in cols:
    cur.execute("ALTER TABLE matches ADD COLUMN reg_open_until_start BOOLEAN DEFAULT 0")
    con.commit()
    print("ADDED matches.reg_open_until_start")
else:
    print("column already exists — skip")
# sanity: show current states
for row in cur.execute("SELECT id, title, starts_at, reg_open_until_start FROM matches ORDER BY id"):
    print(row)
con.close()
print("OK")
