# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1 AS build
WORKDIR /app

COPY package.json bun.lock* ./

# use ignore-scripts to avoid building node modules like better-sqlite3
RUN bun install --frozen-lockfile --ignore-scripts

# Copy the entire project
COPY . .

RUN bun --bun run build

# copy production dependencies and source code into final image
FROM oven/bun:1 AS production
WORKDIR /app

# Only `.output` folder is needed from the build stage
COPY --from=build /app/.output /app

# Copy migration SQL files for runtime migrations
COPY --from=build /app/drizzle/migrations /app/drizzle/migrations

# Copy entrypoint script
COPY --from=build /app/scripts/start.ts /app/scripts/start.ts

# Copy package.json and lockfile for production dependency install
COPY --from=build /app/package.json /app/package.json
COPY --from=build /app/bun.lock* ./

# Install production dependencies only (drizzle-orm, pg, etc.)
RUN bun install --frozen-lockfile --production --ignore-scripts

# run the app
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "/app/scripts/start.ts" ]
