# Backend Dockerfile for Django API application
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first for better caching
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Install requests for health check
RUN pip install --no-cache-dir requests

# Copy application code
COPY . .

# Create data directories for SQLite, token storage, and config storage
RUN mkdir -p data/db data/tokens data/config

# Expose port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1

# Run migrations and start Django
CMD ["sh", "-c", "python manage.py migrate && python manage.py migrate_legacy_auth && python manage.py runserver 0.0.0.0:8000"]

