# apps/wp_invoices/services/pipeline.py

from typing import Any, Dict, Optional
import os

from apps.wp_invoices.services.extractor import run_extraction


def process_invoice_bytes(
    file_bytes: bytes,
    filename: str,
    content_type: str,
    engine: Optional[str] = None,
) -> Dict[str, Any]:
    """
    Orquesta el flujo mínimo de wp_invoices para un archivo:

      1. Usa run_extraction(...) para obtener el JSON de la factura.
      2. (En el futuro) podría llamar analyzer/validation.
      3. Devuelve un dict con:
          - extracted: dict con la factura
          - checks: dict con validaciones (por ahora vacío)

    Parámetros:
      - engine: "mini", "thinking", etc. Si es None, usa el default
        WP_INVOICES_DEFAULT_ENGINE (por ahora 'mini').
    """

    # Si no nos pasaron engine, usamos el default del sistema
    if engine is None:
        engine = os.getenv("WP_INVOICES_DEFAULT_ENGINE", "mini")

    # 1) Ejecutar la extracción con tu extractor.py
    extracted = run_extraction(
        data=file_bytes,
        filename=filename,
        mimetype=content_type,
        engine=engine,
    )

    # 2) Checks mínimos (luego los llenamos con validation.py)
    checks: Dict[str, Any] = {
        "warnings": [],
        "errors": [],
        "engine": engine,
    }

    return {
        "extracted": extracted,
        "checks": checks,
    }
