"""Tests for context_builder — the bootstrapped markdown output."""
import pytest
from bigmind import memory_store
from bigmind.context_builder import build_context, _format_date


@pytest.fixture
def user():
    return memory_store.get_or_create_user("ctxuser", "Context User")


class TestFormatDate:
    def test_valid_iso_date(self):
        assert _format_date("2026-03-30T09:15:00+00:00") == "2026-03-30"

    def test_z_suffix(self):
        assert _format_date("2026-03-30T00:00:00Z") == "2026-03-30"

    def test_none_returns_dash(self):
        assert _format_date(None) == "—"

    def test_empty_string_returns_dash(self):
        assert _format_date("") == "—"


class TestBuildContext:
    def test_returns_string(self, temp_db, user):
        output = build_context(user["id"])
        assert isinstance(output, str)
        assert len(output) > 0

    def test_contains_bigmind_header(self, temp_db, user):
        output = build_context(user["id"])
        assert "🧠 BigMind Context" in output

    def test_no_profile_shows_placeholder(self, temp_db, user):
        output = build_context(user["id"])
        assert "memory_update_profile" in output

    def test_profile_shown_when_set(self, temp_db, user):
        memory_store.upsert_identity_profile(
            user["id"],
            role="Principal Engineer",
            preferences="Python first",
            pinned_facts="- Uses uv for packages",
        )
        output = build_context(user["id"])
        assert "Principal Engineer" in output
        assert "Python first" in output
        assert "Uses uv for packages" in output

    def test_no_sessions_shows_placeholder(self, temp_db, user):
        output = build_context(user["id"])
        assert "No past sessions yet" in output

    def test_closed_session_appears_in_index(self, temp_db, user):
        sid = memory_store.create_session(user["id"])
        memory_store.close_session(
            sid, "Implemented BigMind Phase 1",
            topics="mcp,sqlite", outcome="All files created"
        )
        output = build_context(user["id"])
        assert "Implemented BigMind Phase 1" in output
        assert "mcp,sqlite" in output

    def test_open_session_shown_as_in_progress(self, temp_db, user):
        memory_store.create_session(user["id"])
        output = build_context(user["id"])
        # open session MUST appear — marked as [in progress] for parallel IDE visibility
        assert "in progress" in output
        # but "No past sessions yet" placeholder must NOT appear
        assert "No past sessions yet" not in output

    def test_tier2_hint_shown_for_sessions_with_summary(self, temp_db, user):
        sid = memory_store.create_session(user["id"])
        memory_store.close_session(sid, "Session with summary")
        memory_store.save_session_summary(sid, "Full narrative here")
        output = build_context(user["id"])
        assert "📄" in output

    def test_respects_n_sessions_limit(self, temp_db, user):
        for i in range(15):
            sid = memory_store.create_session(user["id"])
            memory_store.close_session(sid, f"Session {i}")
        output = build_context(user["id"], n_sessions=5)
        assert "last 5" in output

