FROM node:20-alpine AS builder

RUN apk add --no-cache openssl

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY prisma ./prisma

RUN npx prisma generate

COPY . .

RUN npm run build

FROM node:20-alpine AS runtime

RUN apk add --no-cache openssl

WORKDIR /app

COPY package*.json ./

RUN npm ci --omit=dev

COPY prisma ./prisma
RUN npx prisma generate

COPY --from=builder /app/dist ./dist

RUN mkdir -p /app/data

ENV HOST=0.0.0.0
ENV PORT=4321
ENV DATABASE_URL=file:/app/data/dev.db

CMD npx prisma migrate deploy && node ./dist/server/entry.mjs
