"""Tests for daemon utility helpers."""

import os
from unittest.mock import patch

from fliiq.runtime.scheduler.daemon_utils import (
    _read_daemon_version,
    ensure_daemon_running,
    is_daemon_running,
    read_pid,
)


def test_read_pid(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    pid_path = tmp_path / ".fliiq" / "daemon.pid"
    pid_path.write_text("12345")
    assert read_pid(tmp_path) == 12345


def test_read_pid_missing(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    assert read_pid(tmp_path) is None


def test_is_daemon_running_no_pid(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    assert is_daemon_running(tmp_path) is False


def test_is_daemon_running_stale_pid(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    pid_path = tmp_path / ".fliiq" / "daemon.pid"
    pid_path.write_text("999999")  # Almost certainly not a real PID
    assert is_daemon_running(tmp_path) is False


# ── Version-aware daemon restart ─────────────────────────────────────


def test_read_daemon_version(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    (tmp_path / ".fliiq" / "daemon.version").write_text("1.24.0")
    assert _read_daemon_version(tmp_path) == "1.24.0"


def test_read_daemon_version_missing(tmp_path):
    (tmp_path / ".fliiq").mkdir()
    assert _read_daemon_version(tmp_path) is None


def test_ensure_daemon_skips_restart_when_version_matches(tmp_path):
    """Matching version → no restart, returns True immediately."""
    (tmp_path / ".fliiq").mkdir()
    (tmp_path / ".fliiq" / "daemon.pid").write_text(str(os.getpid()))
    (tmp_path / ".fliiq" / "daemon.version").write_text("1.0.0")

    with patch("fliiq.runtime.scheduler.daemon_utils.fliiq") as mock_fliiq:
        mock_fliiq.__version__ = "1.0.0"
        result = ensure_daemon_running(tmp_path)

    assert result is True


def test_ensure_daemon_restarts_on_version_mismatch(tmp_path):
    """Version mismatch → stop old daemon, start new one."""
    (tmp_path / ".fliiq").mkdir()
    (tmp_path / ".fliiq" / "daemon.pid").write_text(str(os.getpid()))
    (tmp_path / ".fliiq" / "daemon.version").write_text("1.24.0")

    with (
        patch("fliiq.runtime.scheduler.daemon_utils.fliiq") as mock_fliiq,
        patch("fliiq.runtime.scheduler.daemon_utils.stop_daemon") as mock_stop,
        patch("fliiq.runtime.scheduler.daemon_utils.wait_for_health", return_value=True),
        patch("subprocess.Popen"),
    ):
        mock_fliiq.__version__ = "1.25.0"
        result = ensure_daemon_running(tmp_path)

    mock_stop.assert_called_once_with(tmp_path)
    assert result is True


def test_ensure_daemon_restarts_when_version_missing(tmp_path):
    """No version file (old daemon) → restart."""
    (tmp_path / ".fliiq").mkdir()
    (tmp_path / ".fliiq" / "daemon.pid").write_text(str(os.getpid()))
    # No daemon.version file

    with (
        patch("fliiq.runtime.scheduler.daemon_utils.fliiq") as mock_fliiq,
        patch("fliiq.runtime.scheduler.daemon_utils.stop_daemon") as mock_stop,
        patch("fliiq.runtime.scheduler.daemon_utils.wait_for_health", return_value=True),
        patch("subprocess.Popen"),
    ):
        mock_fliiq.__version__ = "1.25.0"
        result = ensure_daemon_running(tmp_path)

    mock_stop.assert_called_once_with(tmp_path)
    assert result is True
