from __future__ import annotations

import datetime
from unittest.mock import MagicMock

from app.config import Settings
from app.models.config import PendingRecovery
from app.models.server import ServerStatus
from app.utils.runtime_state import get_app_settings, get_effective_settings


class _FakeState:
    def __init__(self, settings: Settings, runtime_settings: object | None = None) -> None:
        self.settings = settings
        self.runtime_settings = runtime_settings


class _FakeApp:
    def __init__(self, state: object) -> None:
        self.state = state


def test_get_effective_settings_returns_runtime_settings() -> None:
    settings = Settings(session_secret="secret")
    runtime_settings = settings.model_copy(update={"database_path": "/tmp/runtime.db"})
    app = _FakeApp(_FakeState(settings=settings, runtime_settings=runtime_settings))

    assert get_effective_settings(app) is runtime_settings


def test_get_effective_settings_returns_app_settings_when_runtime_none() -> None:
    settings = Settings(session_secret="secret")
    app = _FakeApp(_FakeState(settings=settings))

    assert get_effective_settings(app) is settings


def test_get_effective_settings_returns_mock_runtime_settings() -> None:
    settings = Settings(session_secret="secret")
    mock_settings = MagicMock()
    app = _FakeApp(_FakeState(settings=settings, runtime_settings=mock_settings))

    assert get_effective_settings(app) is mock_settings


def test_get_app_settings_reads_bootstrap_settings() -> None:
    settings = Settings(session_secret="secret")
    app = _FakeApp(_FakeState(settings=settings))

    assert get_app_settings(app) is settings


def test_process_health_probe_result_creates_pending_recovery_within_window() -> None:
    from app.utils.runtime_state import RuntimeState, process_health_probe_result

    now = datetime.datetime.now(tz=datetime.UTC)
    runtime_state = RuntimeState(
        server_status=ServerStatus(online=True),
        last_activation={"jail_name": "sshd", "at": now - datetime.timedelta(seconds=30)},
        pending_recovery=None,
    )

    process_health_probe_result(runtime_state, ServerStatus(online=False), now=now)

    assert runtime_state.pending_recovery is not None
    assert runtime_state.pending_recovery.jail_name == "sshd"
    assert runtime_state.pending_recovery.recovered is False


def test_process_health_probe_result_resolves_existing_pending_recovery() -> None:
    from app.utils.runtime_state import RuntimeState, process_health_probe_result

    activated_at = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(seconds=30)
    runtime_state = RuntimeState(
        server_status=ServerStatus(online=False),
        pending_recovery=PendingRecovery(
            jail_name="sshd",
            activated_at=activated_at,
            detected_at=activated_at + datetime.timedelta(seconds=10),
            recovered=False,
        ),
    )

    process_health_probe_result(runtime_state, ServerStatus(online=True), now=activated_at + datetime.timedelta(seconds=20))

    assert runtime_state.pending_recovery is not None
    assert runtime_state.pending_recovery.recovered is True
