FROM python:3.11-slim

# Install Node.js
RUN apt-get update && apt-get install -y \
    curl \
    gnupg \
    ffmpeg \
    && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get install -y nodejs \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# --- Backend Setup ---
COPY backend/requirements.txt ./backend/requirements.txt
RUN pip install --no-cache-dir -r backend/requirements.txt

# --- Frontend Setup ---
COPY frontend/package*.json ./frontend/
WORKDIR /app/frontend
# Install dependencies (ignoring peer deps conflicts)
RUN npm install --legacy-peer-deps

COPY frontend/ .
# Build Next.js (with ignore-lint already set in next.config.mjs)
# We set API URL to http://localhost:8000 because in this container strategy, 
# the browser will access the backend directly.
# Wait, for client-side fetches, "localhost" refers to the user's machine.
# If we run this container on port 3000 and 8000, localhost:8000 works internally via Rewrites.
# ENV NEXT_PUBLIC_API_URL="http://localhost:8000" Removed to use relative path proxying
# Build Next.js
ENV NEXTAUTH_URL=http://localhost:3000
# Secret should be provided at runtime via docker run -e or docker-compose
ARG NEXTAUTH_SECRET_ARG=default_dev_secret_change_in_production
ENV NEXTAUTH_SECRET=${NEXTAUTH_SECRET_ARG}
RUN npm run build

# --- Final Setup ---
WORKDIR /app
COPY backend/ ./backend/

# Create a start script
RUN echo '#!/bin/bash\n\
    uvicorn backend.main:app --host 0.0.0.0 --port 8000 &\n\
    cd frontend && npm start -- -p 3000\n\
    ' > start.sh && chmod +x start.sh

EXPOSE 3000 8000

CMD ["./start.sh"]
