"""Platform-aware service registration dispatcher.

Detects the current OS and delegates to the appropriate backend:
- Windows  -> ``konstant.windows.service``  (pywin32)
- Linux    -> ``konstant.linux.service``    (user-level systemd units)
- macOS    -> ``konstant.darwin.service``   (user-level launchd plists)
"""

from __future__ import annotations

import sys
from pathlib import Path
from typing import Optional

from konstant.config import ServiceConfig


def _platform_label() -> str:
    if sys.platform == "win32":
        return "Windows service"
    elif sys.platform == "darwin":
        return "launchd agent"
    return "systemd user service"


def install_service(
    config: ServiceConfig,
    user: Optional[str] = None,
    password: Optional[str] = None,
    config_path: Optional[Path] = None,
) -> None:
    """Register a konstant-managed process as a system service."""
    if sys.platform == "win32":
        from konstant.windows.service import install_service as _install
    elif sys.platform == "darwin":
        from konstant.darwin.service import install_service as _install
    else:
        from konstant.linux.service import install_service as _install
    _install(config, user=user, password=password, config_path=config_path)


def uninstall_service(service_name: str) -> None:
    """Remove a konstant system service registration."""
    if sys.platform == "win32":
        from konstant.windows.service import uninstall_service as _uninstall
    elif sys.platform == "darwin":
        from konstant.darwin.service import uninstall_service as _uninstall
    else:
        from konstant.linux.service import uninstall_service as _uninstall
    _uninstall(service_name)
