#!/usr/bin/env python3
"""Diagnose page render errors (run inside instance dir with venv)."""
from __future__ import annotations

import os
import sys
import traceback


def main() -> int:
    inst = sys.argv[1] if len(sys.argv) > 1 else "crypto_monitor_binance"
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    os.chdir(os.path.join(root, inst))
    sys.path.insert(0, os.getcwd())
    from app import app  # noqa: WPS433

    paths = ["/trade", "/key_monitor", "/strategy", "/login"]
    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess["logged_in"] = True
        for path in paths:
            try:
                resp = client.get(path)
                print(f"{inst} {path} -> {resp.status_code}")
                if resp.status_code >= 400:
                    body = resp.get_data(as_text=True)
                    print(body[:3000])
            except Exception:
                print(f"{inst} {path} -> EXCEPTION")
                traceback.print_exc()
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
