"""Fail-closed compatibility shim for the retired unsigned updater.

The historical implementation downloaded arbitrary ZIP/Python payloads,
overwrote the running installation, and rebooted without verifying a signed
release manifest. Production updates are exclusively handled by the signed,
transactional OTA service in :mod:`main.integrations.ota`.

The old symbols intentionally remain importable so stale integrations receive
one deterministic error instead of falling back to dangerous behavior.
"""

from __future__ import annotations


DEFAULT_UPDATE_URL = ""
APP_VERSION = "retired"
SERIAL_NUMBER = "unknown"
UPDATE_URL = ""
CONFIG_PATH = "config.json"
MAIN_SCRIPT = "main.py"

LEGACY_OTA_DISABLED_MESSAGE = (
    "Unsigned legacy OTA is retired: use the signed WorkcellServices OTA "
    "integration"
)


class LegacyOTAUpdateDisabledError(RuntimeError):
    """Raised for every entry point of the retired unsigned updater."""


def _disabled(*_args, **_kwargs):
    raise LegacyOTAUpdateDisabledError(LEGACY_OTA_DISABLED_MESSAGE)


def get_update_info_url(
    update_url: str | None = None,
    serial_number: str | None = None,
) -> str:
    _disabled(update_url, serial_number)


def check_for_updates() -> None:
    _disabled()


def perform_update_check() -> None:
    _disabled()


def download_and_install_update(
    url: str,
    expected_checksum: str | None = None,
) -> None:
    _disabled(url, expected_checksum)


def cleanup_old_version(directory: str = "previous_version") -> None:
    _disabled(directory)


def check_latest_version(url: str, session: object | None = None):
    _disabled(url, session)


def download_update_file(url: str, session: object | None = None):
    _disabled(url, session)


def save_update_file(data: bytes, path: str) -> None:
    _disabled(data, path)


def update_version_file(version: str, path: str) -> None:
    _disabled(version, path)


def reboot_device() -> None:
    _disabled()


def read_version(path: str) -> str:
    _disabled(path)


def _parse(version: str) -> tuple[int, ...]:
    """Retain the pure version parser for import compatibility only."""

    return tuple(int(component) for component in version.split("."))


def micro_is_newer(remote: str, current: str) -> bool:
    """Compare versions without initiating an update or any I/O."""

    try:
        return _parse(remote) > _parse(current)
    except (TypeError, ValueError):
        return False


def _load_config(path: str) -> dict:
    _disabled(path)


def _save_config(config: dict, path: str) -> None:
    _disabled(config, path)


def _download(url: str):
    _disabled(url)


def _fetch_json(url: str):
    _disabled(url)


def micro_apply_update(
    data: bytes,
    version: str,
    config_path: str,
    target: str = MAIN_SCRIPT,
) -> None:
    _disabled(data, version, config_path, target)


def micro_check_for_updates(config_path: str = CONFIG_PATH) -> None:
    _disabled(config_path)


def micro_schedule_update(
    interval_ms: int,
    config_path: str = CONFIG_PATH,
    timer_id: int = 0,
):
    _disabled(interval_ms, config_path, timer_id)
