"""Pack API endpoints (read-only for Phase 2)."""

from __future__ import annotations

from typing import Any

from fastapi import APIRouter, HTTPException, Request

from ..services import packs

router = APIRouter(tags=["packs"])


@router.get("/packs")
async def list_packs(request: Request) -> list[dict[str, Any]]:
    """List all installed packs with artifact counts."""
    db = request.app.state.db
    result = packs.list_packs(db)
    for p in result:
        p.pop("source_path", None)
    return result


@router.get("/packs/{namespace}/{name}")
async def get_pack(request: Request, namespace: str, name: str) -> dict[str, Any]:
    """Get a pack with its full artifact list."""
    db = request.app.state.db
    result = packs.get_pack(db, namespace, name)
    if not result:
        raise HTTPException(status_code=404, detail="Pack not found")
    result.pop("source_path", None)
    for art in result.get("artifacts", []):
        art.pop("content", None)
    return result
