"""
Notifications desktop — interface unique, implementations par OS plus tard.

Windows : toast (winotify / plyer)
macOS   : osascript ou pync
Linux   : notify-send
"""

from __future__ import annotations

import sys


def notify(title: str, message: str) -> bool:
    """
    Affiche une notification si la plateforme le supporte.
    Retourne True si une notification a ete tentee avec succes.
    """
    if sys.platform == "darwin":
        return _notify_macos(title, message)
    if sys.platform == "win32":
        return _notify_windows(title, message)
    return _notify_linux(title, message)


def _notify_macos(title: str, message: str) -> bool:
    try:
        import subprocess

        script = f'display notification "{_escape_applescript(message)}" with title "{_escape_applescript(title)}"'
        subprocess.run(
            ["osascript", "-e", script],
            check=True,
            capture_output=True,
        )
        return True
    except (OSError, subprocess.CalledProcessError):
        return False


def _notify_windows(title: str, message: str) -> bool:
    # Placeholder — brancher winotify / plyer quand notifications activees
    return False


def _notify_linux(title: str, message: str) -> bool:
    try:
        import subprocess

        subprocess.run(
            ["notify-send", title, message],
            check=True,
            capture_output=True,
        )
        return True
    except (OSError, subprocess.CalledProcessError, FileNotFoundError):
        return False


def _escape_applescript(text: str) -> str:
    return text.replace("\\", "\\\\").replace('"', '\\"')
