import json

from rich.console import Console

from harbor.constants import NOTIFICATIONS_PATH

REGISTRY_HINT_KEY = "registry-datasets-hint"


def has_seen_notification(key: str) -> bool:
    """Check if a notification has been shown before."""
    if not NOTIFICATIONS_PATH.exists():
        return False
    try:
        data = json.loads(NOTIFICATIONS_PATH.read_text())
        return key in data.get("seen", [])
    except (json.JSONDecodeError, OSError):
        return False


def mark_notification_seen(key: str) -> None:
    """Mark a notification as shown."""
    NOTIFICATIONS_PATH.parent.mkdir(parents=True, exist_ok=True)
    try:
        if NOTIFICATIONS_PATH.exists():
            data = json.loads(NOTIFICATIONS_PATH.read_text())
        else:
            data = {"seen": []}
        if key not in data.get("seen", []):
            data.setdefault("seen", []).append(key)
        NOTIFICATIONS_PATH.write_text(json.dumps(data))
    except (json.JSONDecodeError, OSError):
        pass


def show_registry_hint_if_first_run(console: Console) -> None:
    """Show a hint about available registry datasets on first run."""
    if has_seen_notification(REGISTRY_HINT_KEY):
        return
    mark_notification_seen(REGISTRY_HINT_KEY)
    console.print(
        "[dim]Tip: There are many benchmarks available in Harbor's registry.[/dim]"
    )
    console.print(
        "[dim]Run `harbor datasets list` to see all available datasets.[/dim]"
    )
    console.print()
