FROM node:20-alpine AS build

# Install pnpm
RUN npm install -g pnpm

# Set working directory
WORKDIR /app

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

# Install dependencies
RUN pnpm install --no-frozen-lockfile

# Copy source code
COPY . .

# Build the application
RUN pnpm build

# Create production image
FROM node:20-alpine AS production

# Install pnpm
RUN npm install -g pnpm

# Set working directory
WORKDIR /app

# Copy build artifacts and package files
COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json /app/pnpm-lock.yaml* ./

# Install production dependencies only
RUN pnpm install --prod --no-frozen-lockfile

# Copy .env.example file
COPY .env.example .env.example

# Expose port used by the SSE server
EXPOSE 3000

# Set environment variables
ENV NODE_ENV=production

# Make index.js executable
RUN chmod +x ./dist/index.js

# Image metadata
LABEL org.opencontainers.image.authors="schuettc"
LABEL org.opencontainers.image.url="https://github.com/schuettc/aws-logs-mcp"
LABEL org.opencontainers.image.source="https://github.com/schuettc/aws-logs-mcp"
LABEL org.opencontainers.image.title="AWS Logs MCP"
LABEL org.opencontainers.image.description="Model Context Protocol server for AWS CloudWatch Logs and CloudTrail Events"

# Start the application
CMD ["node", "dist/index.js"]