"""CAD file router - serve thumbnails, glTF models, parsed objects, and trigger reprocessing."""
import uuid
from datetime import datetime
from pathlib import Path

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import FileResponse
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from sqlalchemy.orm import selectinload

from app.database import get_db
from app.models.cad_file import CadFile, ProcessingStatus
from app.models.order import Order
from app.models.order_item import OrderItem
from app.models.user import User
from app.utils.auth import get_current_user
from app.services.product_service import link_cad_to_product, lookup_product

router = APIRouter(prefix="/cad", tags=["cad"])


# ---------------------------------------------------------------------------
# Schemas for match-to-order
# ---------------------------------------------------------------------------

class MatchToOrderRequest(BaseModel):
    order_id: uuid.UUID
    cad_file_ids: list[str]


class MatchedItem(BaseModel):
    item_id: str
    cad_file_id: str
    item_name: str
    cad_name: str


class MatchToOrderResponse(BaseModel):
    matched: list[MatchedItem]
    unmatched_cad: list[str]
    unmatched_items: list[str]


# ---------------------------------------------------------------------------
# Matching helper
# ---------------------------------------------------------------------------

def _normalize_stem(name: str) -> str:
    """Lowercase stem, strip .stp/.step extension for comparison."""
    stem = name.strip()
    for ext in (".step", ".stp"):
        if stem.lower().endswith(ext):
            stem = stem[: -len(ext)]
            break
    return stem.lower()


# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------

@router.post("/match-to-order", response_model=MatchToOrderResponse)
async def match_cad_files_to_order(
    body: MatchToOrderRequest,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """
    Match uploaded CAD files to order items by filename similarity.

    For each CAD file, compares the stem of original_name (case-insensitive,
    .stp/.step normalised) to the stem of each item's name_cad_modell field.
    Updates order_item.cad_file_id for successful matches.
    """
    # Load order with items
    order_result = await db.execute(
        select(Order)
        .where(Order.id == body.order_id)
        .options(selectinload(Order.items))
    )
    order = order_result.scalar_one_or_none()
    if not order:
        raise HTTPException(404, detail="Order not found")
    if user.role.value != "admin" and order.created_by != user.id:
        raise HTTPException(403, detail="Access denied")

    # Parse and validate CAD file IDs
    cad_uuids: list[uuid.UUID] = []
    for raw_id in body.cad_file_ids:
        try:
            cad_uuids.append(uuid.UUID(raw_id))
        except ValueError:
            raise HTTPException(400, detail=f"Invalid cad_file_id: {raw_id}")

    # Load CAD files from DB
    cad_result = await db.execute(
        select(CadFile).where(CadFile.id.in_(cad_uuids))
    )
    cad_files: list[CadFile] = list(cad_result.scalars().all())

    found_ids = {str(cf.id) for cf in cad_files}
    missing = [i for i in body.cad_file_ids if i not in found_ids]
    if missing:
        raise HTTPException(404, detail=f"CAD files not found: {missing}")

    # Build lookup: normalized stem -> first OrderItem with that stem
    items: list[OrderItem] = order.items
    item_by_stem: dict[str, OrderItem] = {}
    for item in items:
        if item.name_cad_modell:
            stem = _normalize_stem(item.name_cad_modell)
            if stem not in item_by_stem:
                item_by_stem[stem] = item

    matched: list[MatchedItem] = []
    unmatched_cad: list[str] = []
    matched_item_ids: set[str] = set()

    for cad_file in cad_files:
        cad_stem = _normalize_stem(cad_file.original_name or "")
        if cad_stem in item_by_stem:
            item = item_by_stem[cad_stem]
            item.cad_file_id = cad_file.id
            item.updated_at = datetime.utcnow()
            matched.append(
                MatchedItem(
                    item_id=str(item.id),
                    cad_file_id=str(cad_file.id),
                    item_name=item.name_cad_modell or "",
                    cad_name=cad_file.original_name or "",
                )
            )
            matched_item_ids.add(str(item.id))

            # Propagate the STEP link to the product so that:
            # (a) the render pipeline can find it via product.cad_file_id
            # (b) future orders for the same product inherit the STEP automatically
            # (c) the split-missing-step correctly identifies which products have STEP
            try:
                product = await lookup_product(db, item.pim_id, item.produkt_baureihe)
                if product and product.cad_file_id is None:
                    await link_cad_to_product(db, product.id, cad_file.id)
            except Exception:
                pass  # non-critical — item link already set above
        else:
            unmatched_cad.append(str(cad_file.id))

    await db.commit()

    unmatched_items = [
        str(item.id)
        for item in items
        if str(item.id) not in matched_item_ids
    ]

    return MatchToOrderResponse(
        matched=matched,
        unmatched_cad=unmatched_cad,
        unmatched_items=unmatched_items,
    )


# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------

async def _get_cad_file(cad_id: uuid.UUID, db: AsyncSession) -> CadFile:
    result = await db.execute(select(CadFile).where(CadFile.id == cad_id))
    cad = result.scalar_one_or_none()
    if not cad:
        raise HTTPException(status_code=404, detail="CAD file not found")
    return cad


@router.get("/{id}/thumbnail")
async def get_thumbnail(
    id: uuid.UUID,
    db: AsyncSession = Depends(get_db),
):
    """Serve the thumbnail image for a CAD file (no auth — UUID is opaque enough)."""
    cad = await _get_cad_file(id, db)

    if not cad.thumbnail_path:
        raise HTTPException(404, detail="Thumbnail not yet generated for this CAD file")

    thumb_path = Path(cad.thumbnail_path)
    if not thumb_path.exists():
        raise HTTPException(404, detail="Thumbnail file missing from storage")

    ext = thumb_path.suffix.lower()
    media_type = "image/jpeg" if ext in (".jpg", ".jpeg") else "image/png"

    return FileResponse(
        path=str(thumb_path),
        media_type=media_type,
        filename=f"{id}{ext}",
    )


@router.get("/{id}/model")
async def get_model(
    id: uuid.UUID,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Serve the glTF file for a CAD file."""
    cad = await _get_cad_file(id, db)

    if not cad.gltf_path:
        raise HTTPException(
            status_code=404,
            detail="glTF model not yet generated for this CAD file",
        )

    gltf_path = Path(cad.gltf_path)
    if not gltf_path.exists():
        raise HTTPException(
            status_code=404,
            detail="glTF file missing from storage",
        )

    # glTF files may be either .gltf (JSON) or .glb (binary)
    suffix = gltf_path.suffix.lower()
    if suffix == ".glb":
        media_type = "model/gltf-binary"
    else:
        media_type = "model/gltf+json"

    return FileResponse(
        path=str(gltf_path),
        media_type=media_type,
        filename=f"{id}{suffix}",
    )


@router.get("/{id}/objects")
async def get_objects(
    id: uuid.UUID,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Return the parsed_objects JSON extracted from the STEP file."""
    cad = await _get_cad_file(id, db)

    if cad.parsed_objects is None:
        raise HTTPException(
            status_code=404,
            detail="Parsed objects not yet available for this CAD file",
        )

    return {
        "cad_file_id": str(cad.id),
        "original_name": cad.original_name,
        "processing_status": cad.processing_status.value,
        "parsed_objects": cad.parsed_objects,
    }


@router.get("/{id}/stl/{quality}")
async def download_stl(
    id: uuid.UUID,
    quality: str,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Download the cached STL for a CAD file with a human-readable filename.

    The STL is cached next to the STEP file on first render.
    quality must be 'low' or 'high'.
    """
    if quality not in ("low", "high"):
        raise HTTPException(400, detail="quality must be 'low' or 'high'")

    cad = await _get_cad_file(id, db)

    if not cad.stored_path:
        raise HTTPException(404, detail="STEP file not uploaded for this CAD file")

    step_path = Path(cad.stored_path)
    stl_path = step_path.parent / f"{step_path.stem}_{quality}.stl"

    if not stl_path.exists():
        raise HTTPException(
            404,
            detail=f"STL cache not found for quality '{quality}'. Trigger a render first to generate it.",
        )

    original_stem = Path(cad.original_name or "model").stem
    filename = f"{original_stem}_{quality}.stl"

    return FileResponse(
        path=str(stl_path),
        media_type="application/octet-stream",
        filename=filename,
    )


@router.post("/{id}/generate-stl/{quality}", status_code=status.HTTP_202_ACCEPTED)
async def generate_stl(
    id: uuid.UUID,
    quality: str,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Queue STL generation for the given quality without triggering a full render."""
    if user.role.value not in ("admin", "project_manager"):
        raise HTTPException(status_code=403, detail="Insufficient permissions")
    if quality not in ("low", "high"):
        raise HTTPException(status_code=400, detail="quality must be 'low' or 'high'")

    cad = await _get_cad_file(id, db)

    if not cad.stored_path:
        raise HTTPException(status_code=404, detail="STEP file not uploaded for this CAD file")

    from app.tasks.step_tasks import generate_stl_cache
    task = generate_stl_cache.delay(str(id), quality)
    return {"status": "queued", "task_id": task.id, "quality": quality}


@router.post(
    "/{id}/regenerate-thumbnail",
    status_code=status.HTTP_202_ACCEPTED,
)
async def regenerate_thumbnail(
    id: uuid.UUID,
    user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    """Queue a Celery task to reprocess the STEP file and regenerate its thumbnail."""
    if user.role.value != "admin":
        raise HTTPException(
            status_code=403,
            detail="Only admins can trigger thumbnail regeneration",
        )

    cad = await _get_cad_file(id, db)

    # Reset processing status so the worker will reprocess
    cad.processing_status = ProcessingStatus.pending
    await db.commit()

    # Enqueue Celery task
    task_id: str | None = None
    try:
        from app.tasks.step_tasks import process_step_file
        result = process_step_file.delay(str(cad.id))
        task_id = result.id
    except Exception:
        # Worker may not be running; status is already reset so it will pick up later
        pass

    return {
        "cad_file_id": str(cad.id),
        "original_name": cad.original_name,
        "status": "queued",
        "task_id": task_id,
    }
