# ============================================================================
# YT Player PWA — Docker image
#
# Base: oven/bun:1-debian (Bun runtime on Debian slim)
# yt-dlp: downloaded from GitHub releases at build time (stays current)
# libsql: embedded via @libsql/client (no separate DB container needed)
#
# Build:  docker compose build
# Run:    docker compose up
# ============================================================================

FROM oven/bun:1-debian

# ---- System dependencies ----
# python3 is required by yt-dlp for some extraction paths
# ca-certificates for HTTPS fetches from yt-dlp
# ffmpeg lets yt-dlp merge bestvideo+bestaudio into a single mp4 for the
# "Save before playing" download path (GET /api/download?mux=1)
RUN apt-get update -qq && \
    apt-get install -y --no-install-recommends \
      curl \
      python3 \
      ffmpeg \
      ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# ---- Install yt-dlp ----
RUN curl -fsSL \
      https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp \
      -o /usr/local/bin/yt-dlp && \
    chmod +x /usr/local/bin/yt-dlp

WORKDIR /app

# ---- Install Node/Bun deps ----
COPY server/package.json ./
RUN bun install --production

# ---- Copy server source ----
COPY server/ ./

# ---- Copy built frontend (served as static files from ./public) ----
COPY frontend/ ./public/

# ---- Stamp the build time (shown in Settings → About) ----
# Runs after the COPY layers, so any source change produces a fresh stamp
# while a fully-cached (unchanged) build keeps its original one.
RUN date -u +"%Y-%m-%dT%H:%M:%SZ" > /app/build-time.txt

# ---- Persistent data directory (volume-mounted) ----
RUN mkdir -p /app/data

EXPOSE 3000

# Healthcheck — ping the version endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD curl -sf http://localhost:3000/api/version || exit 1

CMD ["bun", "server.js"]
