#!/usr/bin/env python3
"""Rebrand DB: replace vapolyx/VAPOLYX strings in all tables' text columns.

Protected (NOT touched): users.email  -> admin@vapolyx.gg is a functional
login credential, not visible branding.
"""
import sqlite3

DB = "/home/z/my-project/vapolyx/data.sqlite"
conn = sqlite3.connect(DB)
conn.row_factory = sqlite3.Row
cur = conn.cursor()

tables = [r[0] for r in cur.execute(
    "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")]

total = 0
for tb in tables:
    cols = cur.execute(f"PRAGMA table_info({tb})").fetchall()
    text_cols = [c["name"] for c in cols
                 if any(k in (c["type"] or "").upper()
                        for k in ("TEXT", "VARCHAR", "STRING", "CHAR"))]
    for col in text_cols:
        if tb == "users" and col == "email":
            continue  # protected credential
        try:
            rows = cur.execute(
                f"SELECT rowid, {col} FROM {tb} WHERE {col} LIKE '%vapolyx%' "
                f"OR {col} LIKE '%VAPOLYX%' OR {col} LIKE '%واپولیکس%'").fetchall()
        except sqlite3.OperationalError:
            continue
        for r in rows:
            old = r[1]
            new = (old.replace("VAPOLYX", "FIGHTSKILL")
                      .replace("Vapolyx", "Fightskill")
                      .replace("vapolyx", "fightskill")
                      .replace("واپولیکس", "فایت‌اسکیل"))
            cur.execute(f"UPDATE {tb} SET {col}=? WHERE rowid=?", (new, r[0]))
            print(f"  {tb}.{col} rowid={r[0]}: {old!r} -> {new!r}")
            total += 1

conn.commit()
conn.close()
print(f"\nDone. {total} rows updated.")
