"""Tests for ErrorAnalyzer — ensures raw errors never leak."""

import pytest
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "backend"))

from local_llm.error_analyzer import ErrorAnalyzer


class TestQuickAnalyze:
    """Test rule-based error analysis (no LLM needed)."""

    def test_column_not_found(self):
        result = ErrorAnalyzer.quick_analyze(
            'column "revenue_total" does not exist'
        )
        assert "column" in result.lower()
        assert "does not exist" in result.lower() or "doesn't exist" in result.lower()
        # Must NOT contain the actual column name
        assert "revenue_total" not in result

    def test_table_not_found(self):
        result = ErrorAnalyzer.quick_analyze(
            'relation "v3q_data.missing_table" does not exist'
        )
        assert "table" in result.lower()
        assert "missing_table" not in result

    def test_syntax_error(self):
        result = ErrorAnalyzer.quick_analyze(
            "syntax error at or near 'SELEC'"
        )
        assert "syntax" in result.lower()
        assert "SELEC" not in result

    def test_type_mismatch(self):
        result = ErrorAnalyzer.quick_analyze(
            "cannot cast type integer to text"
        )
        assert "type" in result.lower() or "mismatch" in result.lower()

    def test_group_by_error(self):
        result = ErrorAnalyzer.quick_analyze(
            'column "orders.customer_name" must appear in GROUP BY clause'
        )
        assert "group by" in result.lower()
        assert "customer_name" not in result

    def test_ambiguous_column(self):
        result = ErrorAnalyzer.quick_analyze(
            'column reference "id" is ambiguous'
        )
        assert "ambiguous" in result.lower()

    def test_division_by_zero(self):
        result = ErrorAnalyzer.quick_analyze("division by zero")
        assert "division by zero" in result.lower()

    def test_permission_denied(self):
        result = ErrorAnalyzer.quick_analyze(
            "permission denied for table secret_data"
        )
        assert "permission" in result.lower()
        assert "secret_data" not in result

    def test_timeout(self):
        result = ErrorAnalyzer.quick_analyze("statement timeout")
        assert "too long" in result.lower() or "timeout" in result.lower()

    def test_unknown_error_generic(self):
        result = ErrorAnalyzer.quick_analyze("some completely unknown error xyz123")
        assert "error" in result.lower()
        assert "xyz123" not in result

    def test_no_raw_error_in_output(self):
        """The sanitized output must NEVER contain raw error details."""
        raw_errors = [
            'psycopg2.errors.UndefinedColumn: column "x" does not exist',
            'asyncpg.exceptions.PostgresSyntaxError: syntax error',
            'sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError)',
        ]
        for raw in raw_errors:
            result = ErrorAnalyzer.quick_analyze(raw)
            assert "psycopg" not in result
            assert "asyncpg" not in result
            assert "sqlalchemy" not in result
