"""Integration tests for symbol upload functionality."""

import pytest


class TestSymbolResolutionIntegration:
    """Integration tests for symbol resolution with custom symbols."""

    def test_custom_symbol_precedence_logic(self):
        """Test that custom symbols take precedence over auto-detected."""
        from src.extraction.symbol_resolver import (
            CUSTOM_SYMBOL_CONTEXT,
            SymbolResolver,
        )

        resolver = SymbolResolver()

        # Test _resolve_record directly
        symbol_lookup = {"○": "Yes/Applicable", "×": "No/Not applicable"}
        content = {"field1": "○", "field2": "×", "field3": "Some text"}

        resolved, count, unresolved = resolver._resolve_record(content, symbol_lookup)

        assert resolved is not None
        assert resolved["field1_resolved"] == "Yes/Applicable"
        assert resolved["field2_resolved"] == "No/Not applicable"
        assert "field3_resolved" not in resolved
        assert count == 2
        assert len(unresolved) == 0

    def test_unknown_symbol_detection(self):
        """Test that unknown symbols are detected and reported."""
        from src.extraction.symbol_resolver import SymbolResolver

        resolver = SymbolResolver()

        symbol_lookup = {"○": "Yes"}
        content = {"field1": "◎", "field2": "△"}  # Unknown symbols

        resolved, count, unresolved = resolver._resolve_record(content, symbol_lookup)

        assert resolved is None  # No symbols resolved
        assert count == 0
        assert "◎" in unresolved
        assert "△" in unresolved

    def test_mixed_known_unknown_symbols(self):
        """Test resolution with mix of known and unknown symbols."""
        from src.extraction.symbol_resolver import SymbolResolver

        resolver = SymbolResolver()

        symbol_lookup = {"○": "Yes", "×": "No"}
        content = {"field1": "○", "field2": "◎"}  # One known, one unknown

        resolved, count, unresolved = resolver._resolve_record(content, symbol_lookup)

        assert resolved is not None
        assert resolved["field1_resolved"] == "Yes"
        assert "field2_resolved" not in resolved
        assert count == 1
        assert "◎" in unresolved


class TestReResolveSymbolsTaskIntegration:
    """Integration tests for re_resolve_symbols Celery task."""

    def test_re_resolve_symbols_task_structure(self):
        """Test that re_resolve_symbols task has correct structure."""
        from src.workers.tasks import re_resolve_symbols

        # Verify task is registered correctly
        assert re_resolve_symbols.name == "src.workers.tasks.re_resolve_symbols"
        assert re_resolve_symbols.max_retries == 3

    def test_custom_symbol_context_constant(self):
        """Test that CUSTOM_SYMBOL_CONTEXT is consistent across modules."""
        from src.extraction.symbol_resolver import (
            CUSTOM_SYMBOL_CONTEXT as RESOLVER_CONTEXT,
        )
        from src.services.symbol_service import (
            CUSTOM_SYMBOL_CONTEXT as SERVICE_CONTEXT,
        )
        from src.workers.tasks import CUSTOM_SYMBOL_CONTEXT as TASKS_CONTEXT

        assert RESOLVER_CONTEXT == SERVICE_CONTEXT == TASKS_CONTEXT == "api_upload"
