#!/usr/bin/env python3
"""Interactive Omarchy OS terminal menu & desktop launcher.

Provides immediate access to local models, ROMS knowledge engine,
Goose autonomous sessions, and workflow automation.
"""
import os
import shutil
import subprocess
import sys
import webbrowser

ROMS_PORT = 18082
MODEL_PORT = 18080
EMBER_PORT = 8787

def print_banner():
    print("=" * 60)
    print("        \033[1;36mOMARCHY OS - LOCAL AUTONOMOUS HARNESS\033[0m")
    print("=" * 60)
    print(" 1) Run Autonomous Task (Interactive Goose/Omarchy CLI)")
    print(" 2) Open ROMS Knowledge Studio (Browser - Port 18082)")
    print(" 3) Open Ember Workflow Automation (Browser - Port 8787)")
    print(" 4) Check Production System Services (systemd status)")
    print(" 5) Launch Model Benchmark / Speculative Decoding Check")
    print(" 0) Exit")
    print("=" * 60)

def main():
    while True:
        print_banner()
        choice = input("Select an option [1-5, 0]: ").strip()
        if choice == "1":
            goal = input("\nEnter task goal (empty to cancel): ").strip()
            if not goal:
                continue
            workspace = input("Target workspace path [.]: ").strip() or "."
            # Execute goose or omarchy runner
            cmd = ["goose", "run", "--instructions", goal] if shutil.which("goose") else ["python3", "-m", "app.main", "--goal", goal]
            subprocess.run(cmd, cwd=workspace)
        elif choice == "2":
            url = f"http://127.0.0.1:{ROMS_PORT}/"
            print(f"Opening ROMS Knowledge Studio: {url}")
            webbrowser.open(url)
        elif choice == "3":
            url = f"http://127.0.0.1:{EMBER_PORT}/"
            print(f"Opening Ember Workflow Automation: {url}")
            webbrowser.open(url)
        elif choice == "4":
            print("\n--- Systemd User Services Status ---")
            subprocess.run(["systemctl", "--user", "status", "goose-model", "goose-roms", "omarchy-broker", "--no-pager"])
            input("\nPress Enter to return to menu...")
        elif choice == "5":
            print("\n--- Running Omarchy Self-Check ---")
            subprocess.run(["python3", "-m", "pytest", "tests/test_harness_repos_integrations.py", "-v"])
            input("\nPress Enter to return to menu...")
        elif choice == "0" or choice.lower() in ("q", "quit", "exit"):
            print("Exiting Omarchy Menu.")
            break
        else:
            print("Invalid choice, please select 1-5 or 0.")

if __name__ == "__main__":
    main()
