"""Tests for RegexCheck."""

from __future__ import annotations

import re

import pytest

from ai_qa.checks.regex import RegexCheck
from ai_qa.core.context import Context
from ai_qa.exceptions import ContextValidationError


def test_regex_check_requires_pattern():
    """Test that RegexCheck requires pattern parameter."""
    with pytest.raises(ValueError) as exc_info:
        RegexCheck()
    assert "pattern" in str(exc_info.value)


def test_regex_check_invalid_pattern():
    """Test that RegexCheck rejects invalid regex patterns."""
    with pytest.raises(ValueError) as exc_info:
        RegexCheck(pattern="[invalid(")
    assert "regex" in str(exc_info.value).lower()


def test_regex_check_simple_match():
    """Test RegexCheck with simple pattern match."""
    check = RegexCheck(pattern=r"^\d+$")
    ctx = Context(output="12345")
    result = check.evaluate(ctx)
    assert result.passed is True
    assert result.score == 1.0


def test_regex_check_simple_no_match():
    """Test RegexCheck with simple pattern no match."""
    check = RegexCheck(pattern=r"^\d+$")
    ctx = Context(output="abc")
    result = check.evaluate(ctx)
    assert result.passed is False
    assert result.score == 0.0


def test_regex_check_email_pattern():
    """Test RegexCheck with email pattern."""
    pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    check = RegexCheck(pattern=pattern)

    valid_ctx = Context(output="user@example.com")
    assert check.evaluate(valid_ctx).passed is True

    invalid_ctx = Context(output="not-an-email")
    assert check.evaluate(invalid_ctx).passed is False


def test_regex_check_case_sensitive():
    """Test that RegexCheck is case sensitive by default."""
    check = RegexCheck(pattern=r"^[A-Z]+$")
    assert check.evaluate(Context(output="ABC")).passed is True
    assert check.evaluate(Context(output="abc")).passed is False


def test_regex_check_case_insensitive():
    """Test RegexCheck with case insensitive flag."""
    check = RegexCheck(pattern=r"^[a-z]+$", flags=re.IGNORECASE)
    assert check.evaluate(Context(output="abc")).passed is True
    assert check.evaluate(Context(output="ABC")).passed is True


def test_regex_check_multiline():
    """Test RegexCheck with multiline flag."""
    pattern = r"^hello"
    check = RegexCheck(pattern=pattern, flags=re.MULTILINE)
    multiline_text = "goodbye\nhello world"
    result = check.evaluate(Context(output=multiline_text))
    assert result.passed is True


def test_regex_check_search_vs_match():
    """Test that RegexCheck searches (not matches) the pattern."""
    # search() finds pattern anywhere, match() only at start
    check = RegexCheck(pattern=r"\d+")
    ctx = Context(output="hello 123 world")
    result = check.evaluate(ctx)
    assert result.passed is True


def test_regex_check_requires_string_output():
    """Test RegexCheck requires string output."""
    check = RegexCheck(pattern=r"\d+")
    ctx = Context(output={"number": 123})  # Dict, not a string
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)


def test_regex_check_evidence_includes_pattern():
    """Test that evidence includes the pattern."""
    pattern = r"^[a-z]+$"
    check = RegexCheck(pattern=pattern)
    ctx = Context(output="123")
    result = check.evaluate(ctx)
    assert pattern in result.evidence


def test_regex_check_custom_threshold():
    """Test RegexCheck with custom threshold."""
    check = RegexCheck(pattern=r"\d+", threshold=0.8)
    assert check.threshold == 0.8


def test_regex_check_version():
    """Test RegexCheck has correct version."""
    check = RegexCheck(pattern=r"\d+")
    assert check.version == "1.0.0"


def test_regex_check_name():
    """Test RegexCheck has correct name."""
    check = RegexCheck(pattern=r"\d+")
    assert check.name == "regex"


def test_regex_check_requires_llm():
    """Test RegexCheck does not require LLM."""
    check = RegexCheck(pattern=r"\d+")
    assert check.requires_llm is False


def test_regex_check_complex_pattern():
    """Test RegexCheck with complex regex pattern."""
    # URL pattern
    pattern = r"https?://[^\s]+"
    check = RegexCheck(pattern=pattern)

    valid_ctx = Context(output="Visit https://example.com for more info")
    assert check.evaluate(valid_ctx).passed is True

    invalid_ctx = Context(output="Visit example.com for more info")
    assert check.evaluate(invalid_ctx).passed is False
