# src/dashboard/Dockerfile
# Build context: repo root. Reference from docker-compose.yml as
# `dockerfile: src/dashboard/Dockerfile`.
# Multi-stage build: Node builds frontend, Python serves the dashboard API.

# -------- Stage 1: Frontend builder --------
FROM node:20-alpine AS frontend-builder

WORKDIR /build/frontend
COPY src/dashboard/frontend/package*.json ./
RUN npm ci
COPY src/dashboard/frontend/ ./
RUN npm run build

# -------- Stage 2: Python runtime --------
FROM python:3.12-slim AS runtime

ARG VERSION=0.1.0

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

LABEL org.opencontainers.image.title="VoiceGateway Dashboard" \
      org.opencontainers.image.description="Web dashboard for VoiceGateway with full CRUD" \
      org.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.source="https://github.com/mahimailabs/voicegateway" \
      org.opencontainers.image.authors="Mahimai Raja J" \
      org.opencontainers.image.licenses="MIT"

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd -r dashboard --gid=1000 \
    && useradd -r -g dashboard --uid=1000 --home-dir=/app --shell=/bin/bash dashboard

WORKDIR /app

COPY pyproject.toml README.md ./
# src/ layout: hatchling reads packages = ["src/voicegateway", "src/dashboard"]
# from pyproject so the src/ structure has to be present during pip install.
COPY src/voicegateway/ ./src/voicegateway/
COPY src/dashboard/ ./src/dashboard/
# The wheel force-includes the migrations (pyproject
# [tool.hatch.build.targets.wheel.force-include]), so hatchling needs
# alembic.ini + alembic/ present during pip install, not just at runtime.
COPY alembic.ini ./
COPY alembic/ ./alembic/

ARG SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION}
ENV SETUPTOOLS_SCM_PRETEND_VERSION=${SETUPTOOLS_SCM_PRETEND_VERSION}
RUN pip install --no-cache-dir -e ".[dashboard]"

COPY --from=frontend-builder /build/frontend/dist ./dashboard/frontend/dist

USER dashboard

EXPOSE 9090

# After the dashboard fold-in, the daemon owns both /v1/* (HTTP API)
# and /api/* (dashboard API) on a single port. This image keeps 9090
# for docker-compose compatibility but launches the unified daemon
# bound to that port via VOICEGW_PORT.
ENV VOICEGW_PORT=9090

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD curl -fsS http://localhost:9090/api/overview || exit 1

CMD ["python", "-m", "voicegateway.server.main"]
