"""Windows: NL_O1 DataKubun breakdown. ASCII-only for cmd."""
import sqlite3
import sys

path = sys.argv[1] if len(sys.argv) > 1 else r"data\keiba.db"
year = sys.argv[2] if len(sys.argv) > 2 else "2026"

conn = sqlite3.connect(path)
rk = "JyoCD||MonthDay||RaceNum"

print(f"=== NL_O1 DataKubun {year} ===")
print(f"db: {path}")
print("")
print("DataKubun  rows   races_distinct")
for kubun, rows in conn.execute(
    "SELECT DataKubun, COUNT(*) FROM NL_O1 WHERE Year=? GROUP BY DataKubun ORDER BY DataKubun",
    (year,),
):
  races = conn.execute(
      f"SELECT COUNT(DISTINCT {rk}) FROM NL_O1 WHERE Year=? AND DataKubun=?",
      (year, kubun),
  ).fetchone()[0]
  print(f"{kubun!s:>9}  {rows:>6}  {races:>15}")

def races_with_tanodds(where: str, params: tuple) -> int:
  return conn.execute(
      f"SELECT COUNT(DISTINCT {rk}) FROM NL_O1 WHERE Year=? AND {where} "
      "AND TanOdds IS NOT NULL AND CAST(TanOdds AS REAL) > 0",
      (year, *params),
  ).fetchone()[0]

print("")
print("races with TanOdds (confirmed kubun):")
print("  kubun=5 only:     ", races_with_tanodds("DataKubun=?", ("5",)))
print("  kubun=4 only:     ", races_with_tanodds("DataKubun=?", ("4",)))
print("  kubun in (4,5):   ", races_with_tanodds("DataKubun IN (?,?)", ("4", "5")))

ra = conn.execute("SELECT COUNT(*) FROM NL_RA WHERE Year=?", (year,)).fetchone()
if ra:
  print("")
  print(f"NL_RA races (ref): {ra[0]}")

conn.close()
