"""C8 FcAdapter / GcsAdapter error hierarchy (AZ-390 / E-C8).

Two disjoint trees so consumers can ``except FcAdapterError`` to catch
every flight-controller adapter failure without also catching ground
station failures (and vice versa). Sub-classes carry the specific
contract semantics (e.g. ``SourceSetSwitchNotSupportedError`` is a
subclass of ``SourceSetSwitchError`` so iNav's rejection is catchable
as either form per AC-9).
"""

from __future__ import annotations

__all__ = [
    "FcAdapterConfigError",
    "FcAdapterError",
    "FcEmitError",
    "FcOpenError",
    "GcsAdapterConfigError",
    "GcsAdapterError",
    "GcsEmitError",
    "SigningHandshakeError",
    "SigningKeyExpiredError",
    "SourceSetSwitchError",
    "SourceSetSwitchNotSupportedError",
]


# ---------------------------------------------------------------------
# FC adapter tree


class FcAdapterError(Exception):
    """Base class for every `FcAdapter` failure (Invariant catch-all)."""


class FcOpenError(FcAdapterError):
    """`FcAdapter.open()` failed (port unavailable, signing missing, etc.)."""


class FcEmitError(FcAdapterError):
    """`emit_external_position` / `emit_status_text` failed.

    Raised on non-SPD / NaN covariance (Invariant 4),
    `output.smoothed == True` (Invariant 6), wire-encode failure, and
    write-side OS errors.
    """


class SigningHandshakeError(FcAdapterError):
    """MAVLink 2.0 per-flight signing handshake failed (AP only)."""


class SigningKeyExpiredError(FcAdapterError):
    """The current per-flight signing key has rotated out under us."""


class SourceSetSwitchError(FcAdapterError):
    """`request_source_set_switch()` failed (ACK timeout, REJECTED, etc.)."""


class SourceSetSwitchNotSupportedError(SourceSetSwitchError):
    """`request_source_set_switch()` is not implementable for this FC variant.

    Raised by `Msp2InavAdapter` (iNav has no equivalent of
    `MAV_CMD_SET_EKF_SOURCE_SET`). Sub-classes `SourceSetSwitchError`
    so callers can either catch the specific case or treat it as a
    generic switch failure (AC-9).
    """


class FcAdapterConfigError(FcAdapterError):
    """Bad / mismatched config for an FC adapter.

    Raised at config-load for unknown strategy names (AC-5) and at
    factory build for build-flag-OFF strategies (AC-4); also by
    `Msp2InavAdapter.open(...)` when a non-None `signing_key` is
    passed (Invariant 2 — iNav has no signing per RESTRICT-COMM-2).
    """


# ---------------------------------------------------------------------
# GCS adapter tree


class GcsAdapterError(Exception):
    """Base class for every `GcsAdapter` failure."""


class GcsEmitError(GcsAdapterError):
    """`GcsAdapter.emit_summary` / `emit_status_text` failed."""


class GcsAdapterConfigError(GcsAdapterError):
    """Bad / mismatched config for a GCS adapter.

    Raised at config-load for unknown strategy names and at factory
    build for build-flag-OFF strategies.
    """
