"""Unit tests for input_helper module.

Tests the CJK character input handling and backspace functionality.
"""

import pytest
from prompt_toolkit.application import create_app_session
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.output import DummyOutput

from fwauto.input_helper import get_input


def test_get_input_basic():
    """Test basic input functionality."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing "hello" and pressing Enter
            pipe_input.send_text("hello\n")
            result = get_input("test>")
            assert result == "hello"


def test_get_input_with_whitespace():
    """Test that leading/trailing whitespace is stripped."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing "  hello  " and pressing Enter
            pipe_input.send_text("  hello  \n")
            result = get_input("test>")
            assert result == "hello"


def test_get_input_cjk_chinese():
    """Test Chinese character input."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing Chinese characters
            pipe_input.send_text("一二三四五六\n")
            result = get_input("test>")
            assert result == "一二三四五六"
            assert len(result) == 6  # 6 characters


def test_get_input_cjk_japanese():
    """Test Japanese character input."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing Japanese characters
            pipe_input.send_text("こんにちは\n")
            result = get_input("test>")
            assert result == "こんにちは"
            assert len(result) == 5  # 5 characters


def test_get_input_cjk_korean():
    """Test Korean character input."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing Korean characters
            pipe_input.send_text("안녕하세요\n")
            result = get_input("test>")
            assert result == "안녕하세요"
            assert len(result) == 5  # 5 characters


def test_get_input_cjk_with_backspace():
    """Test CJK character input with backspace.

    This is the critical test for the bug fix.
    Simulate typing "一二三四五六" and pressing backspace 6 times.
    """
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Type 6 Chinese characters
            pipe_input.send_text("一二三四五六")
            # Press backspace 6 times (0x7f is DEL/Backspace)
            for _ in range(6):
                pipe_input.send_bytes(b"\x7f")
            # Press Enter
            pipe_input.send_text("\n")

            result = get_input("test>")
            # After 6 backspaces, all characters should be deleted
            assert result == ""


def test_get_input_cjk_partial_backspace():
    """Test partial backspace on CJK characters."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Type 6 Chinese characters
            pipe_input.send_text("一二三四五六")
            # Press backspace 3 times
            for _ in range(3):
                pipe_input.send_bytes(b"\x7f")
            # Press Enter
            pipe_input.send_text("\n")

            result = get_input("test>")
            # After 3 backspaces, should have "一二三" remaining
            assert result == "一二三"
            assert len(result) == 3


def test_get_input_mixed_cjk_and_ascii():
    """Test mixed CJK and ASCII input with backspace."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Type mixed characters
            pipe_input.send_text("hello世界")
            # Press backspace 2 times (delete "界" and "世")
            for _ in range(2):
                pipe_input.send_bytes(b"\x7f")
            # Press Enter
            pipe_input.send_text("\n")

            result = get_input("test>")
            # After 2 backspaces, should have "hello" remaining
            assert result == "hello"


def test_get_input_emoji():
    """Test emoji character input."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate user typing emoji
            pipe_input.send_text("Hello 👋 World 🌍\n")
            result = get_input("test>")
            assert result == "Hello 👋 World 🌍"


def test_get_input_eof():
    """Test EOF handling (Ctrl+D)."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate EOF (Ctrl+D)
            pipe_input.close()

            with pytest.raises(EOFError):
                get_input("test>")


def test_get_input_keyboard_interrupt():
    """Test KeyboardInterrupt handling (Ctrl+C)."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate Ctrl+C
            pipe_input.send_bytes(b"\x03")

            with pytest.raises(KeyboardInterrupt):
                get_input("test>")


def test_get_input_custom_style():
    """Test custom style tag."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Test different style tags
            pipe_input.send_text("test\n")
            result = get_input("prompt>", style_tag="green")
            assert result == "test"

            pipe_input.send_text("test2\n")
            result = get_input("prompt>", style_tag="yellow")
            assert result == "test2"


def test_get_input_empty_input():
    """Test empty input (just pressing Enter)."""
    with create_pipe_input() as pipe_input:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            # Simulate just pressing Enter
            pipe_input.send_text("\n")
            result = get_input("test>")
            assert result == ""
