"""Quick connectivity test for ClickHouse Cloud."""
import os
import clickhouse_connect
from dotenv import load_dotenv

load_dotenv()

client = clickhouse_connect.get_client(
    host=os.environ["CLICKHOUSE_HOST"],
    port=int(os.environ["CLICKHOUSE_PORT"]),
    username=os.environ["CLICKHOUSE_USER"],
    password=os.environ["CLICKHOUSE_PASSWORD"],
    database=os.environ["CLICKHOUSE_DATABASE"],
    secure=os.environ.get("CLICKHOUSE_SECURE", "true").lower() == "true",
)

print("Server version:", client.server_version)
print("Current database:", client.query("SELECT currentDatabase()").result_rows[0][0])

tables = client.query(
    "SELECT name FROM system.tables WHERE database = currentDatabase() ORDER BY name"
).result_rows
print(f"\nTables in '{os.environ['CLICKHOUSE_DATABASE']}' ({len(tables)}):")
for (t,) in tables:
    print(f"  - {t}")

client.close()
