"""
Market Dashboard — Entry Point.

Usage:
    python main.py backend    → Fetch data, save summary, send Telegram message.
    python main.py frontend   → Launch the Streamlit web dashboard.
"""

import subprocess
import sys
from pathlib import Path


def main() -> None:
    """
    Parses the command and runs either backend or frontend.
    """
    if len(sys.argv) < 2:
        print("Usage: python main.py <backend|frontend>")
        sys.exit(1)

    command = sys.argv[1].lower()

    if command == "backend":
        from backend.backend_app import run_backend
        run_backend()

    elif command == "frontend":
        # Streamlit needs to be launched as a subprocess because it
        # expects to be the main process controlling the script.
        frontend_path = Path(__file__).parent / "frontend" / "frontend_app.py"
        subprocess.run(
            [sys.executable, "-m", "streamlit", "run", str(frontend_path)],
            check=True,
        )

    else:
        print(f"Unknown command: '{command}'")
        print("Usage: python main.py <backend|frontend>")
        sys.exit(1)


if __name__ == "__main__":
    main()