FROM node:20-alpine AS base

# Install pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate

# Set working directory
WORKDIR /app

# Copy package files
COPY package.json pnpm-lock.yaml* ./

FROM base AS deps
RUN pnpm install --frozen-lockfile

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Explicitly run PostCSS/Tailwind build before Next.js build
RUN pnpm dlx tailwindcss -i ./src/styles/globals.css -o ./src/styles/output.css
RUN pnpm build

FROM base AS runner
ENV NODE_ENV=production
ENV PORT=805
ENV HOSTNAME "0.0.0.0"

# Copy necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/src/styles/output.css ./src/styles/output.css

EXPOSE 805

CMD ["node", "server.js"]
