# Single-container ECS Fargate image: nginx (port 8080) + FastAPI + Celery
# Build context: project root (parent of demo-app/)
#
# Usage:
#   docker build -f demo-app/Dockerfile.fargate -t fansifter-fargate .

# ── Stage 1: build frontend ───────────────────────────────────────────────────
# Target is linux/amd64 (ECS Fargate x86_64); mediapipe has no arm64 wheel.
FROM --platform=linux/amd64 oven/bun:1 AS frontend-builder

WORKDIR /build

COPY demo-app/package.json demo-app/bun.lock ./
COPY demo-app/frontend/package.json ./frontend/

WORKDIR /build/frontend
RUN bun install

COPY demo-app/frontend ./

# Empty VITE_API_URL → relative /api/ URLs → nginx proxies them to uvicorn
ARG VITE_API_URL=""
RUN bun run build


# ── Stage 2: production image ─────────────────────────────────────────────────
FROM --platform=linux/amd64 python:3.12-slim

# System packages required by OpenCV / MediaPipe / librosa + nginx + supervisord.
# build-essential / g++ / cmake / python3-dev are needed to compile insightface
# (a transitive dep of fansifter-video-clipper) which has no Python 3.12 wheel.
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender1 \
    libgomp1 \
    libsndfile1 \
    nginx \
    supervisor \
    curl \
    build-essential \
    g++ \
    cmake \
    python3-dev \
    && rm -rf /var/lib/apt/lists/*

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

RUN pip install --no-cache-dir uv

# ── Install fansifter-clipper library ────────────────────────────────────────
COPY lib /opt/fansifter-clipper

# ── Install backend Python dependencies ──────────────────────────────────────
COPY demo-app /app/demo-app

# The workspace pyproject.toml at demo-app/pyproject.toml points to ../lib
# (a dev-machine absolute path that doesn't exist in Docker).
# Remove the [tool.uv.sources] block so uv doesn't try to resolve that path.
# We'll install fansifter-video-clipper manually into the venv afterwards.
RUN python3 -c "import re; p='/app/demo-app/pyproject.toml'; c=open(p).read(); c=re.sub(r'\[tool\.uv\.sources\].*','',c,flags=re.DOTALL).strip()+'\n'; open(p,'w').write(c)"

WORKDIR /app/demo-app/backend

# Create venv and install all locked deps except fansifter-video-clipper
# (--frozen keeps exact versions from uv.lock; --no-dev skips dev tools)
RUN uv sync --frozen --no-dev --no-install-package fansifter-video-clipper

# Install the library into the venv created by uv at the workspace root.
# uv sync installed all transitive deps from the lockfile, so --no-deps
# avoids pip re-resolving / downgrading anything uv locked.
# Use 'uv pip install' because uv venvs don't include pip by default.
WORKDIR /app/demo-app
RUN uv pip install --no-deps /opt/fansifter-clipper

# ── Frontend static files ─────────────────────────────────────────────────────
COPY --from=frontend-builder /build/frontend/dist /app/frontend/dist

# ── Nginx configuration ───────────────────────────────────────────────────────
# Replace the default site (port 80) with our config (port 8080)
COPY demo-app/nginx-fargate.conf /etc/nginx/sites-available/default

# ── Supervisord configuration ─────────────────────────────────────────────────
COPY demo-app/supervisord.conf /etc/supervisor/conf.d/app.conf

# ── Entrypoint ────────────────────────────────────────────────────────────────
COPY demo-app/start.sh /app/start.sh
RUN chmod +x /app/start.sh

# Default media directory; override with MEDIA_ROOT=/mnt/efs for EFS
RUN mkdir -p /app/media && chmod 777 /app/media

EXPOSE 8080

ENTRYPOINT ["/app/start.sh"]
