import json
import shutil
import tempfile
from pathlib import Path

import pytest

from pantheon.chatroom.room import ChatRoom
from pantheon.internal.memory import JSONLBackend, MemoryManager


@pytest.mark.asyncio
async def test_list_chats_skips_corrupted_metadata():
    temp_dir = tempfile.mkdtemp()
    try:
        memory_dir = Path(temp_dir)
        manager = MemoryManager(memory_dir, use_jsonl=True)

        valid = manager.new_memory("Healthy Chat")
        valid.extra_data["last_activity_date"] = "2026-04-08T09:40:00"
        manager.save_one(valid.id)

        broken = manager.new_memory("Broken Chat")
        broken.add_messages([{"role": "user", "content": "still recoverable"}])
        manager.save_one(broken.id)

        broken_meta = memory_dir / f"{broken.id}.meta.json"
        broken_meta.write_text('{"id":"broken","name":"Broken Chat","extra_data":{"a', encoding="utf-8")

        chatroom = ChatRoom.__new__(ChatRoom)
        chatroom.memory_manager = MemoryManager(memory_dir, use_jsonl=True)

        result = await ChatRoom.list_chats(chatroom)

        assert result["success"] is True
        assert [chat["id"] for chat in result["chats"]] == [valid.id]
    finally:
        shutil.rmtree(temp_dir)


@pytest.mark.asyncio
async def test_list_chats_skips_corrupted_legacy_json_metadata():
    temp_dir = tempfile.mkdtemp()
    try:
        memory_dir = Path(temp_dir)
        manager = MemoryManager(memory_dir, use_jsonl=False)

        valid = manager.new_memory("Healthy Legacy Chat")
        valid.extra_data["last_activity_date"] = "2026-04-08T09:40:00"
        manager.save_one(valid.id)

        broken = manager.new_memory("Broken Legacy Chat")
        manager.save_one(broken.id)
        (memory_dir / f"{broken.id}.json").write_text("{broken json", encoding="utf-8")

        chatroom = ChatRoom.__new__(ChatRoom)
        chatroom.memory_manager = MemoryManager(memory_dir, use_jsonl=True)

        result = await ChatRoom.list_chats(chatroom)

        assert result["success"] is True
        assert [chat["id"] for chat in result["chats"]] == [valid.id]
        assert result["skipped_chats"] == [{"id": broken.id, "message": "metadata unreadable"}]
    finally:
        shutil.rmtree(temp_dir)


@pytest.mark.asyncio
async def test_list_chats_uses_metadata_without_loading_jsonl_messages(monkeypatch):
    temp_dir = tempfile.mkdtemp()
    try:
        memory_dir = Path(temp_dir)
        manager = MemoryManager(memory_dir, use_jsonl=True)

        chat = manager.new_memory("Metadata Chat")
        chat.extra_data["last_activity_date"] = "2026-06-12T10:00:00"
        chat.extra_data["project"] = {
            "name": "proj-a",
            "workspace_mode": "isolated",
            "workspace_path": "/workspace/proj-a",
        }
        chat.add_messages([{"role": "user", "content": "history should not be read"}])
        manager.save_one(chat.id)

        chatroom = ChatRoom.__new__(ChatRoom)
        chatroom.memory_manager = MemoryManager(memory_dir, use_jsonl=True)

        def fail_load_messages(self, memory_id):
            raise AssertionError(f"load_messages should not be called for {memory_id}")

        monkeypatch.setattr(JSONLBackend, "load_messages", fail_load_messages)

        result = await ChatRoom.list_chats(chatroom, project_name="proj-a")

        assert result["success"] is True
        assert len(result["chats"]) == 1
        summary = result["chats"][0]
        assert summary["id"] == chat.id
        assert summary["name"] == "Metadata Chat"
        assert summary["workspace_mode"] == "isolated"
        assert summary["workspace_path"] == "/workspace/proj-a"
        assert chat.id not in chatroom.memory_manager.memory_store
    finally:
        shutil.rmtree(temp_dir)
