FROM node:20-alpine

WORKDIR /app

# better-sqlite3 is a native module; alpine (musl) often needs to compile it.
RUN apk add --no-cache python3 make g++

COPY package*.json ./
RUN npm ci

COPY . .

# tsc only emits .ts -> dist; the Drizzle migration files (.sql + meta) are read
# at runtime from <dist>/db/migrations, so copy them alongside the build output.
RUN npm run build && cp -r db/migrations dist/db/migrations

ENV NODE_ENV=production
# Keep the SQLite file on the mounted volume (docker-compose.yml), not inside
# the build output where it wouldn't persist across container rebuilds.
ENV DATA_DIR=/app/data

EXPOSE 3001

CMD ["node", "dist/index.js"]
