# Use official Python runtime as base image
FROM python:3.12-slim AS base

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    UV_PROJECT_ENVIRONMENT=/app/.venv \
    ENVIRONMENT=production \
    DATA_DIR=/data

# Set working directory
WORKDIR /app

# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Copy dependency files
COPY pyproject.toml README.md ./

# Copy application code
COPY src/ ./src/

# Install Python dependencies including all optional dependencies
# This makes the image self-contained and ready for any configuration
RUN uv sync --no-dev --all-extras

# Add virtual environment to PATH
ENV PATH="/app/.venv/bin:$PATH"

# Create data directory for SQLite database
RUN mkdir -p /data && chmod 777 /data

# Add healthcheck
HEALTHCHECK \
    --interval=30s \
    --timeout=30s \
    --start-period=5s \
    --retries=3 \
    CMD python -c "import urllib.request; \
        urllib.request.urlopen('http://localhost:8000/health', timeout=10)"

# Expose port
EXPOSE 8000

# Declare volume for persistent data
VOLUME /data

# Set entrypoint to restrict usage to uvicorn with this specific app
ENTRYPOINT ["uvicorn", "src.baby_monitor.main:app"]

# Default arguments
CMD ["--host", "0.0.0.0", "--port", "8000"]
