"""Tests for the Suggested Cron Jobs feature.

Covers the store (add/dedup/cap/accept/dismiss/latch), catalog seeding, the
blueprint->suggestion bridge, and the shared command handler. Uses an isolated
HERMES_HOME so the real suggestions.json is never touched.
"""

import importlib
from unittest.mock import patch

import pytest

@pytest.fixture
def store(tmp_path, monkeypatch):
    """A cron.suggestions module bound to an isolated HERMES_HOME."""
    home = tmp_path / ".hermes"
    home.mkdir()
    monkeypatch.setenv("HERMES_HOME", str(home))
    import hermes_constants
    importlib.reload(hermes_constants)
    import cron.suggestions as s
    importlib.reload(s)
    return s

def _add(store, key="k1", title="Test", source="catalog", schedule="0 9 * * *"):
    return store.add_suggestion(
        title=title,
        description="desc",
        source=source,
        job_spec={"prompt": "do it", "schedule": schedule, "name": title, "deliver": "origin"},
        dedup_key=key,
    )

class TestStore:
    def test_explicit_file_override_wins_over_profile_home(self, tmp_path, monkeypatch):
        from hermes_constants import (
            reset_hermes_home_override,
            set_hermes_home_override,
        )
        import cron.suggestions as suggestions_mod

        explicit_file = tmp_path / "explicit" / "suggestions.json"
        profile_home = tmp_path / "profile"
        monkeypatch.setattr(suggestions_mod, "SUGGESTIONS_FILE", explicit_file)

        token = set_hermes_home_override(profile_home)
        try:
            _add(suggestions_mod, key="explicit-file")
        finally:
            reset_hermes_home_override(token)

        assert explicit_file.exists()
        assert not (profile_home / "cron" / "suggestions.json").exists()

    def test_profile_override_routes_writes_to_current_home(self, tmp_path):
        from hermes_constants import (
            reset_hermes_home_override,
            set_hermes_home_override,
        )
        import cron.suggestions as suggestions_mod

        profile_a = tmp_path / "profile-a"
        profile_b = tmp_path / "profile-b"

        import_token = set_hermes_home_override(profile_a)
        try:
            importlib.reload(suggestions_mod)
        finally:
            reset_hermes_home_override(import_token)

        runtime_token = set_hermes_home_override(profile_b)
        try:
            _add(suggestions_mod, key="profile-b")
        finally:
            reset_hermes_home_override(runtime_token)

        assert (profile_b / "cron" / "suggestions.json").exists()
        assert not (profile_a / "cron" / "suggestions.json").exists()

    def test_add_and_list_pending(self, store):
        rec = _add(store)
        assert rec is not None
        pending = store.list_pending()
        assert len(pending) == 1
        assert pending[0]["title"] == "Test"
        assert pending[0]["status"] == "pending"

    def test_dedup_blocks_duplicate_pending(self, store):
        assert _add(store, key="dup") is not None
        assert _add(store, key="dup") is None  # same key already pending
        assert len(store.list_pending()) == 1

    def test_dismiss_latches_against_redisplay(self, store):
        _add(store, key="latch")
        assert store.dismiss_suggestion("1") is True
        assert store.list_pending() == []
        # Re-adding the same key is refused (never re-offer a dismissed one).
        assert _add(store, key="latch") is None

    def test_unknown_source_rejected(self, store):
        with pytest.raises(ValueError):
            store.add_suggestion(title="x", description="d", source="bogus", job_spec={}, dedup_key="k")

    def test_usage_source_is_consent_first_self_improvement(self, store):
        """Background review suggestions must stay pending until user acceptance."""
        rec = _add(
            store,
            key="usage:weekly-summary",
            title="Weekly project summary",
            source="usage",
            schedule="0 17 * * 5",
        )

        assert rec is not None
        assert rec["source"] == "usage"
        assert rec["status"] == "pending"
        assert rec["job_spec"]["schedule"] == "0 17 * * 5"
        assert store.list_pending()[0]["dedup_key"] == "usage:weekly-summary"

    def test_pending_cap(self, store):
        for i in range(store.MAX_PENDING):
            assert _add(store, key=f"k{i}") is not None
        # One past the cap is dropped.
        assert _add(store, key="over") is None
        assert len(store.list_pending()) == store.MAX_PENDING

    def test_accept_creates_job_and_marks_accepted(self, store):
        _add(store, key="acc", title="My Job")
        created = {}

        def fake_create_job(**kwargs):
            created.update(kwargs)
            return {"id": "job123", "name": kwargs.get("name"), **kwargs}

        with patch("cron.jobs.create_job", fake_create_job):
            job = store.accept_suggestion("1", origin={"platform": "telegram", "chat_id": "5"})

        assert job is not None
        assert created["schedule"] == "0 9 * * *"
        assert created["origin"] == {"platform": "telegram", "chat_id": "5"}
        # No longer pending.
        assert store.list_pending() == []
        # And accepting again is a no-op (not pending anymore).
        assert store.accept_suggestion("acc") is None

    def test_registration_failure_marks_suggestion_accepted(self, store):
        """Retrying an acceptance must not create a duplicate durable job."""
        from cron.scheduler import CronSchedulerRegistrationError

        rec = _add(store, key="registration-failed", title="My Job")
        job = {"id": "job123", "name": "My Job"}
        failure = CronSchedulerRegistrationError(job, RuntimeError("private detail"))

        with patch(
            "cron.scheduler.create_job_with_scheduler_registration",
            side_effect=failure,
        ):
            with pytest.raises(CronSchedulerRegistrationError):
                store.accept_suggestion(rec["id"])

        assert store.list_pending() == []
        assert store.accept_suggestion(rec["id"]) is None

    def test_get_by_id_and_index_and_title(self, store):
        rec = _add(store, key="byref", title="Findable")
        assert store.get_suggestion(rec["id"])["id"] == rec["id"]
        assert store.get_suggestion("1")["id"] == rec["id"]
        assert store.get_suggestion("findable")["id"] == rec["id"]
        assert store.get_suggestion("nope") is None

    def test_clear_resolved_drops_accepted_only(self, store):
        _add(store, key="a")
        _add(store, key="b")
        store.dismiss_suggestion("2")  # b dismissed (retained for latch)
        with patch("cron.jobs.create_job", lambda **k: {"id": "j"}):
            store.accept_suggestion("1")  # a accepted
        removed = store.clear_resolved()
        assert removed == 1  # only the accepted record pruned
        # Dismissed record retained so its dedup_key still latches.
        assert _add(store, key="b") is None

class TestCatalog:
    def test_seed_registers_all_entries(self, store):
        from cron.suggestion_catalog import CATALOG, seed_catalog_suggestions

        created = seed_catalog_suggestions(add_fn=store.add_suggestion)
        assert len(created) == len(CATALOG)
        assert len(store.list_pending()) == min(len(CATALOG), store.MAX_PENDING)

    def test_no_catalog_prompt_bakes_in_absolute_script_path(self):
        from cron.suggestion_catalog import CATALOG, classify_items_script_path

        # Absolute install paths go stale after relocation and don't exist on
        # remote terminal backends (Docker/Modal); prompts must reference
        # scripts by module path instead.
        for entry in CATALOG:
            assert classify_items_script_path() not in entry.job_spec.get("prompt", ""), entry.key

class TestBlueprintBridge:
    def test_blueprint_registers_suggestion(self, store):
        from tools.blueprints import BlueprintSpec, register_blueprint_suggestion

        spec = BlueprintSpec(skill_name="morning-brief", schedule="0 8 * * *", deliver="telegram")
        with patch("cron.suggestions.add_suggestion", store.add_suggestion):
            rec = register_blueprint_suggestion(spec)
        assert rec is not None
        assert rec["source"] == "blueprint"
        assert rec["job_spec"]["skills"] == ["morning-brief"]
        assert rec["job_spec"]["schedule"] == "0 8 * * *"

class TestCommandHandler:
    def test_bare_lists_pending(self, store):
        _add(store, key="c1", title="Daily thing")
        with patch("cron.suggestions.list_pending", store.list_pending):
            from hermes_cli.suggestions_cmd import handle_suggestions_command
            # Patch the module the handler imports.
            with patch.dict("sys.modules"):
                out = handle_suggestions_command("")
        assert "Daily thing" in out
