#!/usr/bin/env python3
"""Show MySQL grants for MYSQL_USER from .env (WSL helper)."""
import asyncio
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent


def load_env() -> None:
    env_path = ROOT / ".env"
    if not env_path.exists():
        return
    for line in env_path.read_text(encoding="utf-8").splitlines():
        line = line.strip()
        if line and not line.startswith("#") and "=" in line:
            k, v = line.split("=", 1)
            os.environ.setdefault(k.strip(), v.strip())


async def main() -> int:
    import aiomysql

    load_env()
    user = os.environ.get("MYSQL_USER", "Nexus")
    host = os.environ.get("MYSQL_HOST", "127.0.0.1")
    port = int(os.environ.get("MYSQL_PORT", "3306"))
    password = os.environ.get("MYSQL_PASSWORD", "")
    db = os.environ.get("MYSQL_DATABASE", "nexus")

    conn = await aiomysql.connect(
        host=host, port=port, user=user, password=password, db=db, connect_timeout=15
    )
    async with conn.cursor() as cur:
        await cur.execute("SHOW GRANTS")
        print(f"Grants for {user}@{host}:")
        for row in await cur.fetchall():
            print(" ", row[0])
    conn.close()
    return 0


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))
