import base64
import os
import urllib.request


credentials = f"{os.environ['V2_STATS_USER']}:{os.environ['V2_STATS_PASS']}"
request = urllib.request.Request("http://127.0.0.1:5511/")
request.add_header("Authorization", "Basic " + base64.b64encode(credentials.encode()).decode())
with urllib.request.urlopen(request, timeout=20) as response:
    page = response.read().decode("utf-8")
checks = {
    "http_page_nonempty": len(page) > 1000,
    "weekday_buttons": page.count("class='weekday-detail-link'") == 7,
    "weekday_dialogs": page.count("class='asset-dialog weekday-dialog'") == 7,
    "weekday_trade_rows": page.count("data-weekday-trade-row='1'") == 108,
    "monday_dialog": "<h2>Lunedì</h2>" in page,
    "sunday_dialog": "<h2>Domenica</h2>" in page,
}
for name, passed in checks.items():
    print(f"{name}={'OK' if passed else 'FAIL'}")
if not all(checks.values()):
    raise SystemExit(1)
