# Stage 1: Build React frontend
FROM node:22-alpine AS frontend
WORKDIR /app/web/frontend
COPY web/frontend/package.json web/frontend/package-lock.json ./
RUN npm ci
COPY web/frontend/ ./
RUN npm run build

# Stage 2: Python app
FROM python:3.12-slim
WORKDIR /app

# Install Python deps
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt gunicorn

# Copy app source
COPY . .

# Copy built frontend from stage 1
COPY --from=frontend /app/web/frontend/dist /app/web/frontend/dist

# Create runtime directories
RUN mkdir -p /app/state/queue /app/media/music /app/media/podcasts /app/media/fallback /app/logs

# Initialize the database
RUN python scripts/init_db.py /app/state/radio.db

EXPOSE 3000

CMD ["gunicorn", "--bind", "0.0.0.0:3000", "--workers", "2", "--timeout", "120", "web.app:app"]
