"""
Pytest configuration for operator tests.

Mocks module-level imports that may not be available in test environment.
"""

import sys
from unittest.mock import MagicMock

# Create a mock BaseOperator class that behaves like a real class
class MockBaseOperator:
    """Mock BaseOperator that can be subclassed."""
    def __init__(self, *args, **kwargs):
        # Store task_id from kwargs
        self.task_id = kwargs.get('task_id', 'mock_task')
        self.log = MagicMock()

    def execute(self, context):
        raise NotImplementedError


# Mock decorator that returns the class unchanged
def mock_apply_defaults(func):
    return func


# Mock Airflow modules before any operator imports
mock_airflow_models = MagicMock()
mock_airflow_models.BaseOperator = MockBaseOperator

mock_airflow_utils_decorators = MagicMock()
mock_airflow_utils_decorators.apply_defaults = mock_apply_defaults

mock_airflow_providers_s3 = MagicMock()

sys.modules['airflow'] = MagicMock()
sys.modules['airflow.models'] = mock_airflow_models
sys.modules['airflow.utils'] = MagicMock()
sys.modules['airflow.utils.decorators'] = mock_airflow_utils_decorators
sys.modules['airflow.providers'] = MagicMock()
sys.modules['airflow.providers.amazon'] = MagicMock()
sys.modules['airflow.providers.amazon.aws'] = MagicMock()
sys.modules['airflow.providers.amazon.aws.hooks'] = MagicMock()
sys.modules['airflow.providers.amazon.aws.hooks.s3'] = mock_airflow_providers_s3

# Mock src.* submodules whose imports pull in heavy deps not available in this
# test env (docling, llama_index, etc). We deliberately do NOT mock `src` itself
# — the real package must remain importable so other test files (e.g.
# tests/airflow/test_breakers.py) can use real submodules like
# `src.resilience.breakers`. Python's import system honors sys.modules['src.X']
# whether the parent `src` is real or mocked.
sys.modules['src.core'] = MagicMock()
sys.modules['src.core.config'] = MagicMock()
sys.modules['src.core.exceptions'] = MagicMock()

# Mock extraction_v2 modules
sys.modules['src.extraction_v2'] = MagicMock()
sys.modules['src.extraction_v2.document_converter'] = MagicMock()
sys.modules['src.extraction_v2.html_table_extractor'] = MagicMock()
sys.modules['src.extraction_v2.llm_header_detector'] = MagicMock()
sys.modules['src.extraction_v2.record_builder'] = MagicMock()
sys.modules['src.extraction_v2.semantic_chunker'] = MagicMock()
sys.modules['src.extraction_v2.large_table_extractor'] = MagicMock()

# Mock knowledge modules
sys.modules['src.knowledge'] = MagicMock()
sys.modules['src.knowledge.embeddings'] = MagicMock()
sys.modules['src.knowledge.structured_store'] = MagicMock()
sys.modules['src.knowledge.vector_store'] = MagicMock()

# Mock extraction modules
sys.modules['src.extraction'] = MagicMock()
sys.modules['src.extraction.models'] = MagicMock()

# Mock db modules
sys.modules['src.db'] = MagicMock()
sys.modules['src.db.session'] = MagicMock()

# Mock plugins/models modules (Airflow plugin models)
sys.modules['models'] = MagicMock()
sys.modules['models.xcom_schemas'] = MagicMock()

# Mock docling modules
sys.modules['docling'] = MagicMock()
sys.modules['docling.document_converter'] = MagicMock()
sys.modules['docling_core'] = MagicMock()
sys.modules['docling_core.transforms'] = MagicMock()
sys.modules['docling_core.transforms.chunker'] = MagicMock()
sys.modules['docling_core.transforms.chunker.base'] = MagicMock()
sys.modules['docling_core.transforms.chunker.hierarchical_chunker'] = MagicMock()
sys.modules['docling_core.transforms.chunker.hybrid_chunker'] = MagicMock()
sys.modules['docling_core.transforms.chunker.tokenizer'] = MagicMock()
sys.modules['docling_core.transforms.chunker.tokenizer.openai'] = MagicMock()
sys.modules['docling_core.transforms.serializer'] = MagicMock()
sys.modules['docling_core.transforms.serializer.base'] = MagicMock()
sys.modules['docling_core.transforms.serializer.common'] = MagicMock()
sys.modules['docling_core.transforms.serializer.markdown'] = MagicMock()
sys.modules['docling_core.types'] = MagicMock()
sys.modules['docling_core.types.doc'] = MagicMock()
sys.modules['docling_core.types.doc.document'] = MagicMock()
