# -*- coding: utf-8 -*-
"""
QwenPaw Chat module P0-level end-to-end test cases.

P0 criteria:
- Core user flows
- Combined coverage across multiple features
- Realistic user scenarios
- High-priority feature verification

Framework: pytest + Playwright + Page Object Pattern.
Run: pytest tests/test_chat_p0.py -v
"""
from __future__ import annotations

import logging
import pytest
from playwright.sync_api import Page, expect, TimeoutError

from pages.chat_page import ChatPage
from config.settings import config
from utils.helpers import (
    log_test_step,
    log_test_result,
    take_screenshot,
    assert_text_contains,
)


logger = logging.getLogger(__name__)


# ============================================================================
# P0-001: New chat + basic Q&A + copy message (core flow combination)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p0
@pytest.mark.chat_core
class TestNewChatAndBasicQA:
    """
    P0-001: New chat + basic text Q&A + copy message.

    Coverage:
    1. New chat (CHAT-001)
    2. Basic text Q&A (CHAT-002)
    3. Copy message (CHAT-008)
    4. Markdown rendering verification

    Business scenario:
    The user opens the Chat page, creates a new chat, sends a question,
    receives an AI response, and copies the response for other uses.
    """

    @pytest.mark.test_id("P0-001")
    def test_new_chat_basic_qa_copy(self, clean_chat_page: ChatPage, request: pytest.FixtureRequest):
        """
        Verify the full flow: new chat, send message, receive response, copy message.

        Steps:
        1. Open the Chat page
        2. Click the New Chat button
        3. Verify the welcome screen
        4. Send a basic text message
        5. Wait for the AI response
        6. Verify message display
        7. Copy the AI response
        8. Verify the message history
        """
        test_name = request.node.name
        log_test_step("1. Open the Chat page")
        clean_chat_page.open()

        log_test_step("2. Click the New Chat button")
        clean_chat_page.create_new_chat()

        log_test_step("3. Verify the welcome screen")
        assert clean_chat_page.verify_welcome_screen(), "Welcome screen not shown"

        log_test_step("4. Send a basic text message")
        clean_chat_page.send_message("你好，请介绍一下你自己")

        log_test_step("5. Wait for the AI response")
        ai_message = clean_chat_page.wait_for_ai_response(timeout=90000)
        assert ai_message is not None, "AI response timed out"

        log_test_step("6. Verify message display")
        user_messages = clean_chat_page.get_user_messages()
        ai_messages = clean_chat_page.get_ai_messages()
        assert len(user_messages) >= 1, "User message not shown"
        assert len(ai_messages) >= 1, "AI message not shown"

        log_test_step("7. Copy the AI response")
        copy_success = clean_chat_page.copy_last_message()
        # Copy is optional; do not require success

        log_test_step("8. Verify the message history")
        all_messages = clean_chat_page.get_all_messages()
        assert len(all_messages) >= 2, "Message history is incomplete"

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")




# ============================================================================
# P0-002: Multi-turn conversation + context understanding (core intelligence combination)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p0
@pytest.mark.chat_context
class TestMultiTurnConversation:
    """
    P0-002: Multi-turn conversation + context understanding.

    Coverage:
    1. Multi-turn conversation (CHAT-004)
    2. Context understanding and memory

    Business scenario:
    The user has a multi-turn conversation; the AI must understand
    context and reply coherently.
    """

    @pytest.mark.test_id("P0-002")
    def test_multi_turn_context_awareness(
        self,
        clean_chat_page: ChatPage,
        request: pytest.FixtureRequest,
    ):
        """
        Verify the AI handles context correctly in multi-turn chat.

        Steps:
        1. Open the Chat page and create a new chat
        2. Send the first-round message
        3. Send a context-dependent follow-up
        4. Verify the conversation history is complete
        """
        test_name = request.node.name
        conversation_flow = [
            "1+1等于几？请直接回答数字",
            "再加2呢？请直接回答数字",
            "这个结果是奇数还是偶数？请简短回答",
        ]

        log_test_step("1. Open the Chat page and create a new chat")
        clean_chat_page.open().create_new_chat()

        log_test_step("2-3. Send multi-turn messages")
        for i, message in enumerate(conversation_flow, 1):
            log_test_step(f"  Turn {i}: send message - {message[:30]}...")
            clean_chat_page.send_message(message)
            ai_response = clean_chat_page.wait_for_ai_response(timeout=90000)
            assert ai_response is not None, f"AI response timed out at turn {i}"

        log_test_step("4. Verify the conversation history is complete")
        ai_messages = clean_chat_page.get_ai_messages()
        assert len(ai_messages) == len(conversation_flow), \
            f"AI message count mismatch: expected {len(conversation_flow)}, actual {len(ai_messages)}"

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed with {len(conversation_flow)} turns")




# ============================================================================
# P0-003: File upload + file-content Q&A (core feature combination)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p0
@pytest.mark.chat_file
class TestFileUploadAndQA:
    """
    P0-003: Attachment upload + file-content Q&A.

    Coverage:
    1. Attachment upload (CHAT-007)
    2. File preview
    3. Intelligent Q&A based on file content

    Business scenario:
    The user uploads a document and then asks questions about its content.
    """

    @pytest.mark.test_id("P0-003")
    def test_upload_file_and_ask_questions(
        self,
        clean_chat_page: ChatPage,
        test_file: str,
        request: pytest.FixtureRequest,
    ):
        """
        Verify Q&A based on uploaded file content.

        Steps:
        1. Open the Chat page
        2. Upload a file
        3. Verify the file upload succeeded
        4. Ask a question based on the file content
        5. Verify the AI response contains file-related content
        """
        test_name = request.node.name

        log_test_step("1-2. Open the Chat page")
        clean_chat_page.open()

        log_test_step("3. Upload a file")
        clean_chat_page.upload_file(test_file)

        log_test_step("4. Verify the file upload succeeded")
        assert clean_chat_page.verify_file_uploaded(timeout=10000), "File upload failed"

        log_test_step("5. Ask a question based on the file content")
        clean_chat_page.send_message("这个文档的标题是什么？请直接回答")
        ai_response = clean_chat_page.wait_for_ai_response(timeout=60000)
        assert ai_response is not None, "AI response timed out"

        log_test_step("6. Verify the AI response contains file-related content")
        response_text = clean_chat_page.get_message_text(ai_response)
        assert len(response_text.strip()) > 0, f"AI response is empty"
        logger.info(f"AI response: {response_text[:200]}")

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")




# ============================================================================
# P0-004: Session management (rename + pin + delete + switch)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p0
@pytest.mark.chat_session
class TestSessionManagement:
    """
    P0-004: Session management comprehensive test.

    Coverage:
    1. View session list
    2. Rename session
    3. Pin session
    4. Delete session
    5. Switch session

    Business scenario:
    The user manages multiple sessions: renaming, pinning important
    sessions, deleting unused ones, and switching between sessions.
    """

    @pytest.mark.test_id("P0-004")
    def test_session_rename_pin_delete_switch(
        self,
        clean_chat_page: ChatPage,
        request: pytest.FixtureRequest,
    ):
        """
        Verify the full session lifecycle management.

        Steps:
        1. Open the Chat page
        2. Create the first session and send a message
        3. Create the second session and send a message
        4. Open the session list and verify the count
        5. Rename the first session
        6. Pin the first session and verify pinned state
        7. Switch to another session and verify its content
        8. Delete the last session and verify deletion succeeded
        """
        test_name = request.node.name

        log_test_step("1. Open the Chat page")
        clean_chat_page.open()
        # Close any lingering dropdown/overlay so it does not block the button
        clean_chat_page.page.keyboard.press("Escape")
        clean_chat_page.page.wait_for_timeout(500)

        log_test_step("2. Create the first session and send a message")
        clean_chat_page.create_new_chat()
        clean_chat_page.send_message_and_wait("1+1等于几")

        log_test_step("3. Create the second session and send a message")
        clean_chat_page.create_new_chat()
        clean_chat_page.send_message_and_wait("2+3等于几")

        log_test_step("4. Open the session list and verify the count")
        # Close any lingering dropdown/overlay
        clean_chat_page.page.keyboard.press("Escape")
        clean_chat_page.page.wait_for_timeout(300)
        clean_chat_page.open_session_list()

        initial_count = clean_chat_page.get_session_count()
        assert initial_count >= 2, f"Not enough sessions: {initial_count}"

        log_test_step("5. Rename the first session")
        clean_chat_page.rename_session(0, "已重命名的测试会话")

        log_test_step("6. Pin the first session and verify pinned state")
        clean_chat_page.pin_session(0)
        assert clean_chat_page.verify_pinned_session(), "Pinned marker not shown"

        log_test_step("7. Switch to another session and verify its content")
        clean_chat_page.switch_to_session(1)
        clean_chat_page.close_session_list()

        messages = clean_chat_page.get_all_messages()
        assert len(messages) > 0, "Session has no messages after switching"

        log_test_step("8. Delete the last session and verify deletion succeeded")
        clean_chat_page.open_session_list()
        count_before = clean_chat_page.get_session_count()
        clean_chat_page.delete_session(count_before - 1)

        count_after = clean_chat_page.get_session_count()
        assert count_after == count_before - 1, \
            f"Delete failed: before {count_before}, after {count_after}"

        clean_chat_page.close_session_list()

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")
    



# ============================================================================
# P0-005: Model switching + skill invocation + agent switching (advanced feature combination)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p1
@pytest.mark.chat_advanced
class TestAdvancedFeatures:
    """
    P0-005: Advanced feature combination.

    Coverage:
    1. Model selection and switching (CHAT-005)
    2. Agent switching (CHAT-006)
    3. Skill invocation (CHAT-011 ~ CHAT-022)
    4. Tool call detail expand/collapse (CHAT-009)

    Business scenario:
    The user switches between models as needed, invokes skills to
    complete specific tasks, and inspects tool call details.
    """

    @pytest.mark.test_id("P0-005")
    def test_model_switch_and_skill_invocation(
        self,
        clean_chat_page: ChatPage,
        request: pytest.FixtureRequest,
    ):
        """
        Verify model switching and skill invocation.

        Steps:
        1. Open the Chat page
        2. Open the model selector
        3. Select a different model (if multiple are available)
        4. Send /skills command to inspect available skills
        5. Verify the skill list is displayed
        6. Test tool call detail expand/collapse
        """
        test_name = request.node.name

        log_test_step("1. Open the Chat page")
        clean_chat_page.open()

        log_test_step("2. Open the model selector")
        clean_chat_page.open_model_selector()

        log_test_step("3. Select the Qwen 3.5 model")
        models = clean_chat_page.get_available_models()
        logger.info(f"Available models: {models}")

        # Prefer qwen3.5plus to make sure the model supports chat
        target_model = None
        for model in models:
            if "3.5" in model and "plus" in model.lower():
                target_model = model
                break

        if target_model:
            clean_chat_page.select_model(target_model)
            clean_chat_page.wait(1000)
            logger.info(f"Switched to model: {target_model}")
        else:
            logger.info("Qwen 3.5 model not found; using the current default model")
            clean_chat_page.page.keyboard.press("Escape")
            clean_chat_page.wait(500)

        log_test_step("4. Send a message using the current model and verify the response")
        clean_chat_page.send_message("1+1等于几？请直接回答数字")
        model_response = clean_chat_page.wait_for_ai_response(timeout=60000)
        assert model_response is not None, "No response after switching models"
        model_response_text = clean_chat_page.get_message_text(model_response)
        assert len(model_response_text.strip()) > 0, "AI response empty after switching models"
        logger.info(f"Model response: {model_response_text[:200]}")

        log_test_step("5. Send a skills query")
        clean_chat_page.send_message("你有哪些技能？请简要列举")
        skills_response = clean_chat_page.wait_for_ai_response(timeout=60000)
        assert skills_response is not None, "No response to skills query"

        log_test_step("6. Verify the skill list is displayed")
        response_text = clean_chat_page.get_message_text(skills_response)
        assert len(response_text.strip()) > 0, "Skills response is empty"
        logger.info(f"Skills response: {response_text[:200]}")

        log_test_step("7. Test tool call detail expand/collapse")
        expanded = clean_chat_page.expand_tool_details()
        if expanded:
            logger.info("Tool details expanded successfully")
            clean_chat_page.expand_tool_details()

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")




# ============================================================================
# P0-006: Input validation + quick actions + error handling (edge-case combination)
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p2
@pytest.mark.chat_validation
class TestInputValidationAndEdgeCases:
    """
    P0-006: Input validation and edge cases.

    Coverage:
    1. Special character handling
    2. Code block input handling

    Business scenario:
    Verify the system handles special characters and code-block input.
    """

    @pytest.mark.test_id("P0-006")
    def test_input_validation_and_special_chars(
        self,
        clean_chat_page: ChatPage,
        request: pytest.FixtureRequest,
    ):
        """
        Verify special character and code block input handling.

        Steps:
        1. Open the Chat page
        2. Test special character input
        3. Test code block input
        """
        test_name = request.node.name

        log_test_step("1. Open the Chat page")
        try:
            clean_chat_page.open()
        except Exception:
            logger.warning("Chat page first load timed out, retrying...")
            clean_chat_page.page.wait_for_timeout(3000)
            clean_chat_page.page.goto(f"{clean_chat_page.base_url}/chat", wait_until="load", timeout=60000)
            clean_chat_page.page.wait_for_timeout(3000)

        log_test_step("2. Test special characters")
        special_chars = "!@#$%^&*()_+-=[]{}|;:',.<>?/`~中文测试🚀"
        clean_chat_page.send_message(special_chars)
        special_response = clean_chat_page.wait_for_ai_response(timeout=30000)
        assert special_response is not None, "No AI response for special-character message"
        special_text = clean_chat_page.get_message_text(special_response)
        assert len(special_text.strip()) > 0, "AI response empty for special-character message"

        user_messages = clean_chat_page.get_user_messages()
        assert len(user_messages) >= 1, "Special-character message not shown in the chat"

        log_test_step("3. Test code block input")
        code_input = """```python
def hello():
    print("Hello, World!")
```"""
        clean_chat_page.send_message(code_input)
        code_response = clean_chat_page.wait_for_ai_response(timeout=30000)
        assert code_response is not None, "No AI response for code-block message"
        code_text = clean_chat_page.get_message_text(code_response)
        assert len(code_text.strip()) > 0, "AI response empty for code-block message"

        all_messages = clean_chat_page.get_all_messages()
        assert len(all_messages) >= 4, f"Message history incomplete: expected at least 4, actual {len(all_messages)}"

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")




# ============================================================================
# P0-007: Message search
# ============================================================================

@pytest.mark.integration
@pytest.mark.requires_llm
@pytest.mark.p1
@pytest.mark.chat_core
class TestChatMessageSearch:
    """
    P0-007: Message search.

    Coverage:
    1. Open the search panel
    2. Enter a keyword to search
    3. Verify search results
    4. Click a result to jump to the corresponding message
    5. Close the search panel

    Business scenario:
    In a long conversation the user uses search to quickly locate
    messages containing a specific keyword.
    """

    @pytest.mark.test_id("P0-007")
    def test_chat_message_search(
        self,
        clean_chat_page: ChatPage,
        request: pytest.FixtureRequest,
    ):
        """
        Verify message search.

        Steps:
        1. Open the Chat page and create a new chat
        2. Send a message containing a specific keyword
        3. Wait for the AI response
        4. Click the search button to open the search panel
        5. Type the keyword in the search box
        6. Verify the search results contain matches
        7. Click a search result to jump to the corresponding message
        8. Close the search panel
        """
        test_name = request.node.name
        search_keyword = "Playwright"

        log_test_step("1. Open the Chat page and create a new chat")
        clean_chat_page.open()
        clean_chat_page.create_new_chat()

        log_test_step("2. Send a message containing the specific keyword")
        clean_chat_page.send_message(f"请简单介绍一下 {search_keyword} 自动化测试框架的核心特点")

        log_test_step("3. Wait for the AI response")
        ai_response = clean_chat_page.wait_for_ai_response(timeout=30000)
        assert ai_response is not None, "AI response timed out"

        log_test_step("4. Open the All Chats drawer (conversation search lives here)")
        # The message-level search panel (ChatSearchPanel) was removed from
        # the UI in the v2.0.0 redesign: ChatActionGroup no longer renders a
        # search trigger and the panel is mounted nowhere. Search now filters
        # SESSIONS by title inside the right-side "All Chats" drawer, so we
        # verify that behaviour (no-match empties the list, clear restores it).
        clean_chat_page.page.keyboard.press("Escape")
        clean_chat_page.wait(300)
        clean_chat_page.open_session_list()
        initial_count = clean_chat_page.get_session_count()
        assert initial_count >= 1, f"No sessions available to search: {initial_count}"
        logger.info(f"Session count before search: {initial_count}")

        log_test_step("5. Search a term that matches no conversation")
        no_match_term = "zzqx_nomatch_e2e_9911"
        clean_chat_page.search_sessions(no_match_term)
        filtered_count = clean_chat_page.get_session_count()
        assert filtered_count == 0, (
            f"No-match search should empty the session list, got {filtered_count}"
        )
        logger.info("No-match term filtered the conversation list to empty")

        log_test_step("6. Clear the search and verify the list is restored")
        clean_chat_page.clear_session_search()
        restored_count = clean_chat_page.get_session_count()
        assert restored_count >= initial_count, (
            f"Conversation list not restored after clearing search: "
            f"expected >= {initial_count}, got {restored_count}"
        )
        logger.info(f"Session count restored: {restored_count}")

        log_test_step("7. Close the drawer")
        clean_chat_page.close_session_list()

        log_test_result(test_name, True, 0)
        logger.info(f"Test {test_name} passed")


# ============================================================================

# ============================================================================
# CHAT-P1-003: Message edit / regenerate
# ============================================================================

@pytest.mark.integration
@pytest.mark.p1
@pytest.mark.chat
class TestChatMessageEdit:
    """
    CHAT-P1-003: Message edit / regenerate.

    Coverage:
    1. After sending a message, find the edit/regenerate button
    2. Verify the button exists and is clickable
    """

    @pytest.mark.test_id("CHAT-P1-003")
    def test_chat_message_edit(self, page: Page, request: pytest.FixtureRequest):
        """Test message edit / regenerate."""
        test_name = request.node.name

        log_test_step("Navigate to the Chat page")
        page.goto(f"{config.base_url}/chat", wait_until="domcontentloaded", timeout=60000)
        page.wait_for_timeout(3000)

        log_test_step("Find the message input area")
        input_area = page.locator(
            'textarea, [class*="chatInput"], [class*="messageInput"], '
            '[contenteditable="true"]'
        ).first

        if input_area.count() == 0:
            logger.info("Message input area not found, skipping test")
            log_test_result(test_name, True, 0)
            return

        log_test_step("Find action buttons on existing messages")
        message_actions = page.locator(
            'button:has(.anticon-edit), button:has(.anticon-redo), '
            'button[aria-label*="edit"], button[aria-label*="retry"], '
            'button[aria-label*="regenerate"], '
            '[class*="messageAction"] button, '
            '[class*="actionBar"] button'
        ).all()

        if len(message_actions) > 0:
            logger.info(f"Found {len(message_actions)} message action button(s)")
            for i, btn in enumerate(message_actions[:3]):
                is_visible = btn.is_visible()
                logger.info(f"Button {i+1}: visible={is_visible}")
        else:
            # Try hovering over a message to trigger the action buttons
            messages = page.locator('[class*="message"], [class*="chatMessage"]').all()
            if len(messages) > 0:
                messages[-1].hover()
                page.wait_for_timeout(1000)
                hover_actions = page.locator(
                    '[class*="actionBar"] button, [class*="messageAction"] button'
                ).all()
                logger.info(f"After hover, found {len(hover_actions)} action button(s)")
            else:
                logger.info("No messages on the page; verify the input area works")
                assert input_area.is_visible(), "Input area should be visible"
                assert input_area.is_enabled(), "Input area should be enabled"
                logger.info("Input area is functional")

        log_test_result(test_name, True, 0)

# ============================================================================
# CHAT-P1-004: Stream interruption / stop generation
# ============================================================================

@pytest.mark.integration
@pytest.mark.p1
@pytest.mark.chat
class TestChatStopGeneration:
    """
    CHAT-P1-004: Stream interruption / stop generation.

    Coverage:
    1. Verify the presence of the stop-generation button
    2. Verify the input area has a send button
    """

    @pytest.mark.test_id("CHAT-P1-004")
    def test_chat_stop_generation(self, page: Page, request: pytest.FixtureRequest):
        """Test stream interruption / stop generation."""
        test_name = request.node.name

        log_test_step("Navigate to the Chat page")
        page.goto(f"{config.base_url}/chat")
        page.wait_for_load_state("domcontentloaded")
        page.wait_for_timeout(3000)

        log_test_step("Verify the input area exists")
        input_area = page.locator(
            'textarea, [class*="chatInput"], [contenteditable="true"]'
        ).first
        assert input_area.count() > 0, "Chat page should have an input area"
        expect(input_area).to_be_visible(timeout=5000)
        logger.info("Input area exists")

        log_test_step("Find the send button")
        send_btn = page.locator(
            'button:has(.anticon-send), button[aria-label*="send"], '
            'button[aria-label*="发送"], [class*="sendButton"], '
            'button:has(.anticon-arrow-up)'
        ).first
        if send_btn.count() > 0:
            logger.info("Send button exists")
        else:
            # The send button may be triggered by Enter; verify the input area accepts input
            assert input_area.is_enabled(), "Input area should be enabled (can submit with Enter)"
            logger.info("No standalone send button found; input area can submit with Enter")

        log_test_step("Find the stop-generation button (may only show during generation)")
        stop_btn = page.locator(
            'button:has(.anticon-pause), button:has(.anticon-stop), '
            'button[aria-label*="stop"], button[aria-label*="停止"], '
            'button:has-text("Stop"), button:has-text("停止"), '
            '[class*="stopButton"], [class*="stop-button"]'
        ).first

        if stop_btn.count() > 0 and stop_btn.is_visible():
            logger.info("Stop-generation button is currently visible")
        else:
            logger.info("Stop-generation button not currently visible (shown only during streaming; normal)")

        log_test_result(test_name, True, 0)

# ============================================================================
# CHAT-P2-001: Long-message / large-file Q&A performance
# ============================================================================

@pytest.mark.integration
@pytest.mark.p2
@pytest.mark.chat
class TestChatLongMessage:
    """
    CHAT-P2-001: Long-message / large-file Q&A performance.

    Coverage:
    1. Type a very long text into the input box
    2. Verify the input box can accept it
    """

    @pytest.mark.test_id("CHAT-P2-001")
    def test_chat_long_message(self, page: Page, request: pytest.FixtureRequest):
        """Test very long message input."""
        test_name = request.node.name

        log_test_step("Navigate to the Chat page")
        page.goto(f"{config.base_url}/chat")
        page.wait_for_load_state("domcontentloaded")
        page.wait_for_timeout(3000)

        log_test_step("Find the input box")
        input_area = page.locator(
            'textarea, [class*="chatInput"], [contenteditable="true"]'
        ).first
        if input_area.count() == 0:
            logger.info("Input box not found, skipping test")
            log_test_result(test_name, True, 0)
            return

        log_test_step("Type a very long text")
        long_text = "这是一段测试文本。" * 200
        input_area.fill(long_text)
        page.wait_for_timeout(1000)

        filled_value = input_area.input_value() if input_area.evaluate('el => el.tagName') == 'TEXTAREA' else input_area.inner_text()
        assert len(filled_value) > 100, f"Long-text input failed; actual length: {len(filled_value)}"
        logger.info(f"Long-text input succeeded; length: {len(filled_value)}")

        # Clear the input
        input_area.fill("")
        page.wait_for_timeout(500)

        log_test_result(test_name, True, 0)

# ============================================================================
# CHAT-P2-002: IME composition event handling
# ============================================================================

@pytest.mark.integration
@pytest.mark.p2
@pytest.mark.chat
class TestChatIMEInput:
    """
    CHAT-P2-002: IME composition event handling.

    Coverage:
    1. Verify the input box supports Chinese input
    2. Verify the input box does not submit during IME composition
    """

    @pytest.mark.test_id("CHAT-P2-002")
    def test_chat_ime_input(self, page: Page, request: pytest.FixtureRequest):
        """Test IME composition events."""
        test_name = request.node.name

        log_test_step("Navigate to the Chat page")
        page.goto(f"{config.base_url}/chat")
        page.wait_for_load_state("domcontentloaded")
        page.wait_for_timeout(3000)

        log_test_step("Find the input box")
        input_area = page.locator(
            'textarea, [class*="chatInput"], [contenteditable="true"]'
        ).first
        if input_area.count() == 0:
            logger.info("Input box not found, skipping test")
            log_test_result(test_name, True, 0)
            return

        log_test_step("Simulate Chinese input")
        input_area.click()
        page.wait_for_timeout(500)

        # Type Chinese text directly
        input_area.fill("你好世界")
        page.wait_for_timeout(500)

        filled_value = input_area.input_value() if input_area.evaluate('el => el.tagName') == 'TEXTAREA' else input_area.inner_text()
        assert "你好世界" in filled_value, f"Chinese input failed: {filled_value}"
        logger.info(f"Chinese input succeeded: {filled_value}")

        # Clear
        input_area.fill("")
        page.wait_for_timeout(500)

        log_test_result(test_name, True, 0)

# ============================================================================
# APPROVAL-001/002/003: Session-level tool approval toggle (upstream #5685)
# ============================================================================

@pytest.mark.integration
@pytest.mark.chat
@pytest.mark.tool_approval
class TestToolApproval:
    """Session-level tool approval toggle in the chat composer.

    Covers the ApprovalLevelToggle shipped in upstream #5685:
    - APPROVAL-001: the toggle renders, offers exactly 4 levels, and the Tag
      text follows the selection.
    - APPROVAL-002: the chosen level is persisted in localStorage and survives
      a page reload.
    - APPROVAL-003: deleting a session clears its ``approval_level-<id>`` key.

    None of these require an LLM — the toggle only writes localStorage and
    injects ``request_context.approval_level`` into outbound requests.
    """

    @pytest.mark.p0
    @pytest.mark.test_id("APPROVAL-001")
    def test_approval_toggle_renders_and_switches(
        self, clean_chat_page: ChatPage, request: pytest.FixtureRequest
    ):
        """Toggle renders, exposes 4 levels, and the Tag follows the choice."""
        test_name = request.node.name

        log_test_step("1. Open the Chat page")
        chat = clean_chat_page.open()

        log_test_step("2. Locate the approval toggle in the composer")
        toggle = chat.get_approval_toggle()
        expect(toggle).to_be_visible(timeout=10000)

        log_test_step("3. Open the menu and assert exactly 4 levels")
        chat.open_approval_menu()
        items = chat.get_approval_menu_items()
        assert len(items) == 4, f"expected 4 approval levels, got {len(items)}"
        chat.page.keyboard.press("Escape")
        chat.wait(300)

        log_test_step("4. Select each level and verify the Tag text follows")
        for level, (en_label, _zh) in ChatPage.APPROVAL_LEVELS.items():
            chat.select_approval_level(level)
            expect(chat.get_approval_toggle()).to_contain_text(en_label)

        log_test_result(test_name, True, 0)

    @pytest.mark.p0
    @pytest.mark.test_id("APPROVAL-002")
    def test_approval_level_persists_across_reload(
        self, clean_chat_page: ChatPage, request: pytest.FixtureRequest
    ):
        """A selected level is stored in localStorage and survives a reload."""
        test_name = request.node.name

        log_test_step("1. Open the Chat page")
        chat = clean_chat_page.open()

        log_test_step("2. Select STRICT and confirm the Tag updates")
        chat.select_approval_level("STRICT")
        expect(chat.get_approval_toggle()).to_contain_text("Strict Mode")

        log_test_step("3. Assert the choice is written to localStorage")
        entries = chat.get_approval_storage_entries()
        assert "STRICT" in entries.values(), f"STRICT not persisted: {entries}"

        log_test_step("4. Reload and assert the level persists")
        chat.page.reload()
        chat.page.locator(chat.CHAT_INPUT).first.wait_for(
            state="visible", timeout=30000
        )
        expect(chat.get_approval_toggle()).to_contain_text("Strict Mode")
        entries_after = chat.get_approval_storage_entries()
        assert "STRICT" in entries_after.values(), (
            f"STRICT lost after reload: {entries_after}"
        )

        log_test_result(test_name, True, 0)

    @pytest.mark.p2
    @pytest.mark.test_id("APPROVAL-003")
    def test_approval_level_cleared_on_session_delete(
        self,
        clean_chat_page: ChatPage,
        api_context,
        request: pytest.FixtureRequest,
    ):
        """Deleting a session removes its ``approval_level-<id>`` localStorage key."""
        test_name = request.node.name

        log_test_step("1. Seed a real chat via API so a deletable row exists (no LLM)")
        seed = api_context.post(
            "/api/chats",
            data={
                "name": "E2E Approval Cleanup",
                "user_id": config.test.user_id,
                "channel": config.test.channel,
                "session_id": f"{config.test.channel}:{config.test.user_id}",
            },
        )
        if not seed.ok:
            pytest.skip(
                f"chat seed failed ({seed.status}); cannot test delete cleanup"
            )

        log_test_step("2. Open chat, open the session list, select the seeded session")
        chat = clean_chat_page.open()
        chat.open_session_list()
        if chat.get_session_count() == 0:
            pytest.skip("seeded session not visible in drawer; skipping cleanup check")
        chat.switch_to_session(0)
        chat.wait(500)

        log_test_step("3. Select STRICT and capture the persisted approval key(s)")
        chat.select_approval_level("STRICT")
        entries = chat.get_approval_storage_entries()
        strict_keys = [k for k, v in entries.items() if v == "STRICT"]
        assert strict_keys, f"no STRICT approval_level entry after select: {entries}"

        log_test_step("4. Delete the session via the more-menu")
        chat.open_session_list()
        chat.delete_session(0)
        chat.wait(800)

        log_test_step("5. Assert the approval key(s) were cleared on delete")
        after = chat.get_approval_storage_entries()
        for k in strict_keys:
            assert k not in after, (
                f"approval key {k} not cleared after delete; still present: {after}"
            )

        log_test_result(test_name, True, 0)


if __name__ == "__main__":
    pytest.main([
        __file__,
        "-v",
        "--tb=short",
        "-m", "p0",
    ])
