FROM python:3.12-slim

WORKDIR /app
ENV DATABASE_URL=sqlite:////data/fin.db

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

RUN useradd -m -u 1000 app && mkdir -p /data && chown app:app /data
USER app

COPY --chown=app:app . .

EXPOSE 8000

# Validate DB access, run migrations, then start the server.
CMD sh -c "python -m app.preflight && alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"
