# Stage 1: Dependencies
FROM oven/bun:1.1 AS deps

WORKDIR /app

# Copy package files
COPY package.json bun.lockb* ./

# Install dependencies
RUN bun install --frozen-lockfile

# Stage 2: Development
FROM oven/bun:1.1 AS development

WORKDIR /app

# Install postgresql-client for database health check
RUN apt-get update && apt-get install -y postgresql-client && rm -rf /var/lib/apt/lists/*

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules

# Copy source code
COPY . .

# Make entrypoint and trigger scripts executable
RUN chmod +x /app/docker-entrypoint.sh /app/scripts/apply-triggers.sh

# Environment Variables
ENV NODE_ENV=production \
    PORT=3001 \
    HOST=0.0.0.0 \
    DATABASE_URL=postgresql://postgres:postgres@localhost:5432/ticketing_db \
    JWT_SECRET=your-super-secret-jwt-key-change-in-production \
    JWT_EXPIRES_IN=86400 \
    REFRESH_TOKEN_EXPIRES_IN=604800 \
    BLOCKCHAIN_RPC_URL=http://localhost:8545 \
    EVENT_MANAGEMENT_ADDRESS=0x0000000000000000000000000000000000000000 \
    EVENT_TICKET_ADDRESS=0x0000000000000000000000000000000000000000 \
    PRICE_ORACLE_ADDRESS=0x0000000000000000000000000000000000000000 \
    RATE_LIMIT_WINDOW_MS=60000 \
    RATE_LIMIT_MAX_REQUESTS=100 \
    MINIO_ENDPOINT=localhost \
    MINIO_PORT=9000 \
    MINIO_USE_SSL=false \
    MINIO_ROOT_USER=minioadmin \
    MINIO_ROOT_PASSWORD=minioadmin \
    MINIO_BUCKET=ticketing-uploads \
    MINIO_PUBLIC_URL=http://localhost:9000 \
    SMTP_HOST=smtp-relay.brevo.com \
    SMTP_PORT=587 \
    SMTP_USER=your-brevo-login-email@example.com \
    SMTP_PASS=your-xsmtp-api-key-from-dashboard \
    SMTP_FROM="TixChain <noreply@yourdomain.com>" \
    FRONTEND_URL=http://localhost:3000 \
    PINATA_JWT=your_pinata_jwt \
    PINATA_API_URL=https://api.pinata.cloud \
    PINATA_GATEWAY_URL=https://gateway.pinata.cloud/ipfs \
    ORACLE_PRIVATE_KEY=your_private_key \
    TAX_API_URL=https://api.pajak.go.id/v1/regional-tax \
    TAX_API_KEY=your_tax_api_key_here \
    TICKETMASTER_API_KEY=your_ticketmaster_api_key \
    GOOGLE_PLACES_API_KEY=your_google_places_api_key \
    LASTFM_API_KEY=your_lastfm_api_key

# Expose port
EXPOSE 3001

# Start with entrypoint script that runs migrations first
ENTRYPOINT ["/app/docker-entrypoint.sh"]
