FROM node:20-bookworm-slim

# Create application directory
RUN mkdir -p /usr/src/app

# Set environment variables
ENV PORT=3000
ENV NEXT_TELEMETRY_DISABLED=1

WORKDIR /usr/src/app

# Copy package.json and pnpm-lock.yaml
COPY package.json /usr/src/app
COPY pnpm-lock.yaml /usr/src/app

# Install pnpm
RUN corepack enable pnpm

# Install dependencies
RUN pnpm install

# Copy the rest of the application code
COPY . /usr/src/app

# Initialize Database, if it not already exists
RUN pnpm run db

# Build the application
RUN pnpm run build

EXPOSE 3000

# Start the application
CMD ["/bin/sh", "-c", "if [ ! -f ./db/sqlite.db ]; then pnpm db; fi && pnpm start"]
