# Stage 1: Build the application
FROM oven/bun:1 as builder
WORKDIR /app

# Declare build argument for the API key
ARG VITE_GPT_OSS_API_KEY

# Copy configuration files and install dependencies
# This layer is cached to speed up future builds
COPY package.json bun.lockb ./
COPY tsconfig.json tsconfig.node.json ./
COPY vite.config.ts postcss.config.js tailwind.config.ts ./
RUN bun install --frozen-lockfile

# Set it as an environment variable for Vite to pick up during build
ENV VITE_GPT_OSS_API_KEY=$VITE_GPT_OSS_API_KEY

# Copy the rest of the source code
COPY . .

# Build the application
RUN bun run build

# Stage 2: Serve the application with Caddy
FROM caddy:2-alpine

# Copy the Caddyfile to the appropriate location
COPY Caddyfile /etc/caddy/Caddyfile

# Copy the built application from the builder stage to Caddy's serve directory
COPY --from=builder /app/dist /srv

# Expose the port Caddy listens on
EXPOSE 80
