"""
Shared DAG Configuration for TextIQ Airflow DAGs.

This module contains centralized configuration for the document extraction pipeline,
including thresholds, batch sizes, timeouts, and resource pool definitions.
"""

from dataclasses import dataclass
from datetime import timedelta
import os

@dataclass(frozen=True)
class ExtractionConfig:
    """Centralized configuration for extraction pipeline."""

    # Thresholds
    LARGE_TABLE_ROW_THRESHOLD: int = 1000
    MAX_TOKENS_PER_CHUNK: int = 8000
    MIN_TOKENS_FOR_MERGE: int = 100

    # Batch sizes
    EMBEDDING_BATCH_SIZE: int = 16
    MAX_EMBEDDING_API_BATCH: int = 100
    LARGE_TABLE_CHUNK_SIZE: int = 100

    # Timeouts (configurable via environment variables)
    PARSE_TIMEOUT: timedelta = timedelta(minutes=int(os.getenv('AIRFLOW_PARSE_TIMEOUT_MINUTES', '120')))
    EMBEDDING_TIMEOUT: timedelta = timedelta(minutes=int(os.getenv('AIRFLOW_EMBEDDING_TIMEOUT_MINUTES', '60')))
    LLM_TASK_TIMEOUT: timedelta = timedelta(minutes=int(os.getenv('AIRFLOW_LLM_TASK_TIMEOUT_MINUTES', '60')))
    STORE_TIMEOUT: timedelta = timedelta(minutes=int(os.getenv('AIRFLOW_STORE_TIMEOUT_MINUTES', '30')))

    # XCom limits
    MAX_XCOM_SIZE: int = 48 * 1024  # 48KB

    # Paths
    TEMP_DATA_DIR: str = '/opt/airflow/logs'
    EXTERNAL_PYTHON: str = '/opt/airflow/.venv/bin/python'

# Singleton instance
CONFIG = ExtractionConfig()

# Pool definitions (created via Airflow CLI or admin UI)
POOLS = {
    'llm_api_pool': 3,      # Max 3 concurrent LLM API calls
    'embedding_pool': 2,    # Max 2 concurrent embedding tasks
}

# Default arguments applied to all DAGs
default_args = {
    'owner': 'textiq',
    'depends_on_past': False,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 0,
    'retry_delay': timedelta(seconds=30),
    'retry_exponential_backoff': True,
    'max_retry_delay': timedelta(minutes=30),
}

