"""Shared coordinator URL helpers for the macOS app."""

from __future__ import annotations

from pathlib import Path

import yaml

DEFAULT_COORDINATOR_URL = "https://hivecompute.fly.dev"
DEFAULT_RELEASES_URL = "https://github.com/sburl/Ambient/releases"
CONFIG_PATH = Path("~/.ambient/config.yaml").expanduser()


def load_coordinator_url(config_path: Path = CONFIG_PATH) -> str:
    """Read the configured coordinator URL, falling back to the public default."""
    try:
        if config_path.exists():
            data = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
            coordinator_url = str(data.get("coordinator_url", "")).strip()
            if coordinator_url:
                return coordinator_url.rstrip("/")
    except Exception:
        pass
    return DEFAULT_COORDINATOR_URL


def dashboard_url(config_path: Path = CONFIG_PATH) -> str:
    """Build the hosted manager URL for the configured coordinator."""
    return f"{load_coordinator_url(config_path)}/manager/"
