"""
V2 Extraction Pipeline - Docling-based document conversion.

This module implements the V2 extraction approach using Docling for
Excel-to-HTML and Word-to-Markdown conversion with LLM-based header detection.

Key Components:
- DocumentToHtmlConverter: Converts Excel to HTML and Word to Markdown using Docling ✅
- BorderTableDetector: Detects table boundaries and inserts separators ✅
- HtmlTableExtractor: Extracts tables from HTML structure ✅
- LlmHeaderDetector: Uses LLM to detect multi-level headers ✅
- RecordBuilder: Builds structured records from HTML tables ✅
- ExtractionPipelineV2: Orchestrates the V2 extraction workflow ✅
- LargeTableExtractor: Handles very large tables (>1000 rows) with chunked processing ✅

Usage:
    from src.extraction_v2 import ExtractionPipelineV2
    from uuid import uuid4

    # For Excel documents
    pipeline = ExtractionPipelineV2()
    result = await pipeline.process_document(uuid4(), "file.xlsx")

    if result.success:
        print(f"Extracted {result.record_count} records!")
    else:
        print(f"Failed: {result.error} - fallback to V1")

    # For large Excel files with automatic detection
    result = await pipeline.process_document_with_large_table_detection(
        uuid4(), "large_file.xlsx"
    )
"""

__version__ = "2.0.0"

# Import core modules that don't have heavy dependencies
from src.extraction_v2.detect_border import BorderTableDetector
from src.extraction_v2.large_table_extractor import (
    LargeTableExtractor,
    LargeTableConfig,
    LargeTableResult,
    extract_large_tables
)

# Import modules that may have optional dependencies with graceful fallback
try:
    from src.extraction_v2.document_converter import (
        DocumentToHtmlConverter,
        convert_excel_to_html,
        convert_word_to_markdown,
        # Keep backward compatibility
        DocumentToHtmlConverter as ExcelToHtmlConverter
    )
    from src.extraction_v2.html_table_extractor import HtmlTableExtractor, HtmlTable, extract_tables_from_html
    from src.extraction_v2.llm_header_detector import LlmHeaderDetector, HeaderStructure, detect_headers_from_html
    from src.extraction_v2.record_builder import RecordBuilder, build_records_from_table
    from src.extraction_v2.pipeline import (
        ExtractionPipelineV2,
        ExtractionResult,
        extract_from_excel,
        extract_from_excel_sync,
        extract_from_excel_with_large_table_detection,
        extract_from_excel_with_large_table_detection_sync
    )
    _FULL_PIPELINE_AVAILABLE = True
except (ImportError, RuntimeError) as e:
    # Pipeline dependencies not available (e.g., missing tokenizer backend)
    import warnings
    warnings.warn(f"Full pipeline not available: {e}. Only LargeTableExtractor and BorderTableDetector will work.")
    _FULL_PIPELINE_AVAILABLE = False
    # Set placeholders
    DocumentToHtmlConverter = None
    ExcelToHtmlConverter = None
    convert_excel_to_html = None
    convert_word_to_markdown = None
    HtmlTableExtractor = None
    HtmlTable = None
    extract_tables_from_html = None
    LlmHeaderDetector = None
    HeaderStructure = None
    detect_headers_from_html = None
    RecordBuilder = None
    build_records_from_table = None
    ExtractionPipelineV2 = None
    ExtractionResult = None
    extract_from_excel = None
    extract_from_excel_sync = None
    extract_from_excel_with_large_table_detection = None
    extract_from_excel_with_large_table_detection_sync = None

__all__ = [
    # Module 1: Document Converter (Excel & Word)
    "DocumentToHtmlConverter",
    "ExcelToHtmlConverter",  # Backward compatibility alias
    "convert_excel_to_html",
    "convert_word_to_markdown",
    # Module 0: Border Detection (preprocessing)
    "BorderTableDetector",
    # Module 2: HTML to Tables
    "HtmlTableExtractor",
    "HtmlTable",
    "extract_tables_from_html",
    # Module 3: LLM Header Detection
    "LlmHeaderDetector",
    "HeaderStructure",
    "detect_headers_from_html",
    # Module 4: Record Builder
    "RecordBuilder",
    "build_records_from_table",
    # Module 5: Pipeline Orchestrator
    "ExtractionPipelineV2",
    "ExtractionResult",
    "extract_from_excel",
    "extract_from_excel_sync",
    "extract_from_excel_with_large_table_detection",
    "extract_from_excel_with_large_table_detection_sync",
    # Module 6: Large Table Extractor
    "LargeTableExtractor",
    "LargeTableConfig",
    "LargeTableResult",
    "extract_large_tables",
]
