FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Install system dependencies
# - build-essential, pkg-config: build native wheels
# - sqlcipher, libsqlcipher-dev: required to build pysqlcipher3
# - libssl-dev: OpenSSL headers used by sqlcipher
# - curl: used by container healthcheck
RUN apt-get update && apt-get install -y \
    build-essential \
    pkg-config \
    sqlcipher \
    libsqlcipher-dev \
    libssl-dev \
    curl \
    && 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

# Copy application code
COPY . .

# Create non-root user
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
USER appuser

# Expose port
EXPOSE 8000

# Health check (requires curl)
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -fsS http://localhost:8000/health || exit 1

# This image is the shared multi-user backend: always run in cloud mode (sign-in required,
# per-user row isolation) even if DATABASE_URL or CLOUD_AUTH_ENABLED are misconfigured.
ENV BUDGII_DEPLOYMENT=cloud

# Run the application with uvicorn
ENV HOST=0.0.0.0
ENV PORT=8000
CMD ["python", "-m", "uvicorn", "main_modular:app", "--host", "0.0.0.0", "--port", "8000", "--log-level", "info"]