"""Tests for timeline_logger.py and learnings_logger.py."""

import json
import os
import sys
from unittest.mock import patch

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))

import learnings_logger
import timeline_logger


def test_timeline_logger_append_list_and_search(tmp_path):
    course_id = "12345"
    with patch.object(timeline_logger, "get_plugin_root",
                      return_value=str(tmp_path)):
        result_one = timeline_logger.append_event(
            course_id,
            "push_started",
            {"object_type": "page", "object_id": "m1-overview"},
        )
        result_two = timeline_logger.append_event(
            course_id,
            "push_verified",
            {"object_type": "page", "object_id": "m1-overview"},
        )

        assert result_one["ok"] is True
        assert result_two["ok"] is True

        ledger_path = tmp_path / "courses" / course_id / "timeline.jsonl"
        lines = ledger_path.read_text(encoding="utf-8").splitlines()
        assert len(lines) == 2

        first_record = json.loads(lines[0])
        assert first_record["event_type"] == "push_started"
        assert first_record["course_id"] == course_id
        assert isinstance(first_record["payload"], dict)

        records = timeline_logger.list_events(course_id)
        assert len(records) == 2

        filtered = timeline_logger.search_events(course_id, "push_verified")
        assert len(filtered) == 1
        assert filtered[0]["payload"]["object_id"] == "m1-overview"


def test_timeline_logger_accepts_mockup_promoted(tmp_path):
    course_id = "12346"
    with patch.object(timeline_logger, "get_plugin_root",
                      return_value=str(tmp_path)):
        result = timeline_logger.append_event(
            course_id,
            "mockup_promoted",
            {
                "pages_promoted": 2,
                "briefs_promoted": 1,
                "staging_paths": ["/tmp/example.html"],
            },
        )

    assert result["ok"] is True


def test_learnings_logger_append_list_and_search(tmp_path):
    course_id = "67890"
    with patch.object(learnings_logger, "get_plugin_root",
                      return_value=str(tmp_path)):
        result_one = learnings_logger.append_learning(
            course_id,
            "tool-restrictions",
            "Canvas strips script tags.",
        )
        result_two = learnings_logger.append_learning(
            course_id,
            "known-course-quirks",
            "Faculty wants unit names spelled out.",
        )

        assert result_one["ok"] is True
        assert result_two["ok"] is True

        ledger_path = tmp_path / "courses" / course_id / "learnings.jsonl"
        lines = ledger_path.read_text(encoding="utf-8").splitlines()
        assert len(lines) == 2

        first_record = json.loads(lines[0])
        assert first_record["category"] == "tool-restrictions"
        assert first_record["promoted_at"] is None
        assert first_record["course_id"] == course_id

        records = learnings_logger.list_learnings(course_id)
        assert len(records) == 2

        filtered = learnings_logger.search_learnings(
            course_id, "known-course-quirks")
        assert len(filtered) == 1
        assert filtered[0]["note"] == "Faculty wants unit names spelled out."
