"""Rutas HTTP del flujo de carga masiva por ZIP."""

from __future__ import annotations

from threading import Thread
import os
from pathlib import Path as FilePath

from fastapi import (
    APIRouter,
    Depends,
    File,
    HTTPException,
    Path,
    Query,
    Request,
    UploadFile,
)
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from typing import Annotated
from pydantic import BaseModel, Field

from starlette import status

from app.auth import get_current_user
from app.core.dependencies import get_services
from app.core.services import AppServices


router = APIRouter()


def _enqueue_batch_file_clinical_materialization(
    *,
    services: AppServices,
    batch_id: str,
    file_id: str,
) -> str:
    dispatcher_mode = os.getenv("BATCH_DISPATCHER", "inprocess").strip().lower()
    if dispatcher_mode == "celery":
        from app.batch_processing.celery_app import materialize_batch_file_job

        result = materialize_batch_file_job.delay(batch_id, file_id)
        services.batch_runtime.queue_batch_file_clinical.execute(
            batch_id,
            file_id,
            job_id=result.id,
        )
        return result.id

    job_id = f"inprocess-{file_id}"
    services.batch_runtime.queue_batch_file_clinical.execute(
        batch_id,
        file_id,
        job_id=job_id,
    )
    Thread(
        target=services.batch_runtime.materialize_batch_file.execute,
        args=(batch_id, file_id),
        daemon=True,
    ).start()
    return job_id


def _enqueue_batch_epicrisis_generation(
    *,
    services: AppServices,
    batch_id: str,
    username: str,
    case_keys: list[str],
) -> str:
    normalized_case_keys = [
        str(case_key or "").strip()
        for case_key in case_keys or []
        if str(case_key or "").strip()
    ]
    dispatcher_mode = os.getenv("BATCH_DISPATCHER", "inprocess").strip().lower()
    if dispatcher_mode == "celery":
        from app.batch_processing.celery_app import generate_all_epicrisis_job

        result = generate_all_epicrisis_job.delay(batch_id, username, normalized_case_keys)
        return result.id

    if not normalized_case_keys:
        return ""

    from app.batch_processing.celery_app import run_case_epicrisis_job

    def _run_inprocess_bulk() -> None:
        for case_key in normalized_case_keys:
            try:
                run_case_epicrisis_job(username, case_key, False)
            except Exception:
                continue
        services.batch_runtime.recompute_batch_bulk_epicrisis.execute(batch_id)

    job_id = f"inprocess-bulk-epicrisis-{batch_id}"
    Thread(target=_run_inprocess_bulk, daemon=True).start()
    return job_id


def _enqueue_batch_epicrisis_excel_generation(
    *,
    services: AppServices,
    batch_id: str,
) -> str:
    dispatcher_mode = os.getenv("BATCH_DISPATCHER", "inprocess").strip().lower()
    if dispatcher_mode == "celery":
        from app.batch_processing.celery_app import generate_batch_epicrisis_excel_job

        result = generate_batch_epicrisis_excel_job.delay(batch_id, "")
        return result.id

    job_id = f"inprocess-batch-epicrisis-excel-{batch_id}"
    Thread(
        target=services.batch_runtime.generate_batch_epicrisis_excel.execute,
        kwargs={"batch_id": batch_id, "job_id": job_id},
        daemon=True,
    ).start()
    return job_id


class ResolveAssociationPayload(BaseModel):
    """Payload mínimo de resolución manual para documentos ambiguos."""

    case_key: str = ""
    patient_name: str = ""
    patient_id: str = ""
    procedure_code: str = ""
    reason: str = Field(..., min_length=1, max_length=500)


def _get_owned_batch(
    *,
    services: AppServices,
    batch_id: str,
    username: str,
) -> dict:
    batch = services.batch_runtime.get_batch_status.execute(batch_id)
    if not batch or batch.get("usuario") != username:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Lote no encontrado"
    )
    return batch


def _get_owned_batch_record(
    *,
    services: AppServices,
    batch_id: str,
    username: str,
) -> dict:
    batch_repository = services.batch_runtime.get_batch_status.batch_repository
    batch = batch_repository.get_batch(batch_id)
    if not batch or batch.get("usuario") != username:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Lote no encontrado"
        )
    return batch


def _serialize_excel_epicrisis_meta(batch: dict) -> dict:
    return {
        "batch_id": batch.get("batch_id") or batch.get("_id") or "",
        "status": batch.get("excel_epicrisis_status", "pendiente"),
        "job_id": batch.get("excel_epicrisis_job_id", ""),
        "requested_at": batch.get("excel_epicrisis_requested_at", ""),
        "generated_at": batch.get("excel_epicrisis_generated_at", ""),
        "error": batch.get("excel_epicrisis_error", ""),
        "filename": batch.get("excel_epicrisis_filename", ""),
        "download_url": batch.get("excel_epicrisis_download_url", ""),
        "included_count": batch.get("excel_epicrisis_included_count", 0),
        "omitted_count": batch.get("excel_epicrisis_omitted_count", 0),
    }


@router.get("/subir_lote", response_class=HTMLResponse)
async def subir_lote_form(
    request: Request,
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
    batch_id: Annotated[str | None, Query()] = None,
):
    """Renderiza la vista Jinja del cargue masivo y su seguimiento."""

    return services.templates.TemplateResponse(
        "subir_lote.html",
        {
            "request": request,
            "user": current_user,
            "batch_id": batch_id or "",
            "dispatcher_name": services.batch_runtime.dispatcher_name,
        },
    )


@router.post("/api/lotes")
async def crear_lote(
    file: Annotated[UploadFile, File(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
):
    """Crea un lote nuevo y responde 202 para seguimiento por polling."""

    try:
        contents = await file.read()
        result = services.batch_runtime.create_batch_upload.execute(
            filename=file.filename or "",
            contents=contents,
            username=current_user.username,
        )
        return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=result)
    except ValueError as exc:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
    except Exception as exc:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"No se pudo crear el lote: {exc}",
        )


@router.get("/api/lotes")
async def listar_lotes_usuario(
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
):
    """Devuelve historial resumido de lotes del usuario autenticado."""

    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "lotes": services.batch_runtime.list_user_batches.execute(
                current_user.username,
                limit=20,
            )
        },
    )


@router.get("/api/lotes/{batch_id}")
async def obtener_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
):
    """Expone el resumen agregado del lote solo para su propietario."""

    batch = _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    batch.pop("usuario", None)
    return batch


@router.get("/api/lotes/{batch_id}/documentos")
async def obtener_documentos_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Lista el detalle por archivo del lote autenticado."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "batch_id": batch_id,
            "documentos": services.batch_runtime.list_batch_files.execute(batch_id),
        },
    )


@router.get("/api/lotes/{batch_id}/pendientes")
async def obtener_pendientes_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Lista archivos ambiguos pendientes de validación manual."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "batch_id": batch_id,
            "pendientes": services.batch_runtime.list_pending_associations.execute(batch_id),
        },
    )


@router.get("/api/lotes/{batch_id}/casos")
async def obtener_casos_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Expone casos consolidados para apoyar resolución manual."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "batch_id": batch_id,
            "casos": services.batch_runtime.list_batch_cases.execute(batch_id),
        },
    )


@router.post("/api/lotes/{batch_id}/epicrisis")
async def generar_epicrisis_masivas_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Encola la generación de todas las epicrisis elegibles del lote."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    try:
        preview = services.batch_runtime.queue_batch_epicrisis.execute(
            batch_id,
            job_id="",
            persist=False,
        )
        job_id = _enqueue_batch_epicrisis_generation(
            services=services,
            batch_id=batch_id,
            username=current_user.username,
            case_keys=preview.get("case_keys", []),
        )
        result = services.batch_runtime.queue_batch_epicrisis.execute(
            batch_id,
            job_id=job_id,
            selected_case_keys=preview.get("case_keys", []),
        )
    except ValueError as exc:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(exc))
    return JSONResponse(
        status_code=status.HTTP_202_ACCEPTED,
        content={
            "batch_id": batch_id,
            "job_id": result.get("job_id", ""),
            "status": result.get("status", "pendiente"),
            "queued_count": result.get("queued_count", 0),
            "skipped_completed_count": result.get("skipped_completed_count", 0),
            "skipped_inflight_count": result.get("skipped_inflight_count", 0),
            "skipped_not_ready_count": result.get("skipped_not_ready_count", 0),
        },
    )


@router.post("/api/lotes/{batch_id}/excel-epicrisis")
async def generar_excel_epicrisis_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Encola la generación del workbook Excel para las epicrisis del lote."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    try:
        services.batch_runtime.queue_batch_epicrisis_excel.execute(
            batch_id,
            job_id="",
            persist=False,
        )
        job_id = _enqueue_batch_epicrisis_excel_generation(
            services=services,
            batch_id=batch_id,
        )
        result = services.batch_runtime.queue_batch_epicrisis_excel.execute(
            batch_id,
            job_id=job_id,
            persist=True,
        )
    except ValueError as exc:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(exc))

    return JSONResponse(
        status_code=status.HTTP_202_ACCEPTED,
        content={
            "batch_id": batch_id,
            "job_id": result.get("job_id", ""),
            "status": result.get("status", "pendiente"),
            "eligible_count": result.get("eligible_count", 0),
            "filename": result.get("filename", ""),
        },
    )


@router.get("/api/lotes/{batch_id}/excel-epicrisis")
async def obtener_excel_epicrisis_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Devuelve metadatos del workbook Excel del lote."""

    batch = _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content=_serialize_excel_epicrisis_meta(batch),
    )


@router.get("/api/lotes/{batch_id}/excel-epicrisis/descarga")
async def descargar_excel_epicrisis_lote(
    batch_id: Annotated[str, Path(...)],
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> FileResponse:
    """Descarga el workbook Excel ya generado para el lote."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    batch = _get_owned_batch_record(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    if str(batch.get("excel_epicrisis_status") or "") not in {
        "completado",
        "completado_con_errores",
    }:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail="El Excel del lote aún no está listo para descarga.",
        )
    report_path = str(batch.get("excel_epicrisis_path") or "").strip()
    if not report_path:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="El Excel del lote aún no está disponible.",
        )
    report_file = FilePath(report_path)
    if not report_file.exists() or not report_file.is_file():
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="No se encontró el archivo Excel del lote.",
        )
    filename = str(batch.get("excel_epicrisis_filename") or report_file.name)
    return FileResponse(
        path=report_file,
        media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        filename=filename,
    )


@router.patch("/api/lotes/{batch_id}/documentos/{file_id}/asociacion")
async def resolver_asociacion_documento(
    batch_id: Annotated[str, Path(...)],
    file_id: Annotated[str, Path(...)],
    payload: ResolveAssociationPayload,
    services: Annotated[AppServices, Depends(get_services)],
    current_user=Depends(get_current_user),
) -> JSONResponse:
    """Resuelve manualmente un documento pendiente y recalcula estado del lote."""

    _get_owned_batch(
        services=services,
        batch_id=batch_id,
        username=current_user.username,
    )
    try:
        result = services.batch_runtime.resolve_file_association.execute(
            batch_id=batch_id,
            file_id=file_id,
            resolved_by=current_user.username,
            case_key=payload.case_key,
            patient_name=payload.patient_name,
            patient_id=payload.patient_id,
            procedure_code=payload.procedure_code,
            reason=payload.reason,
        )
        job_id = _enqueue_batch_file_clinical_materialization(
            services=services,
            batch_id=batch_id,
            file_id=file_id,
        )
        batch = services.batch_runtime.get_batch_status.execute(batch_id) or {}
    except ValueError as exc:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))

    batch.pop("usuario", None)
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "batch_id": batch_id,
            "file": result,
            "job_id": job_id,
            "batch": batch,
        },
    )
