version: '3.8'

services:
  # Frontend - Next.js App
  web:
    image: code.puffinoffset.com/matt/puffin-app:frontend-latest
    container_name: puffin-frontend
    ports:
      - "3800:3000"
    environment:
      - NODE_ENV=production
      # Next.js environment variables (NEXT_PUBLIC_ prefix for client-side)
      - NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL:-https://api.puffinoffset.com}
      - NEXT_PUBLIC_WREN_API_TOKEN=${NEXT_PUBLIC_WREN_API_TOKEN}
      - NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY}
      # Backward compatibility fallbacks (remove after migration)
      - VITE_API_BASE_URL=${VITE_API_BASE_URL}
      - VITE_WREN_API_TOKEN=${VITE_WREN_API_TOKEN}
      - VITE_STRIPE_PUBLISHABLE_KEY=${VITE_STRIPE_PUBLISHABLE_KEY}
    restart: unless-stopped
    networks:
      - puffin-network
    depends_on:
      backend:
        condition: service_healthy

  # Backend - Express API Server
  backend:
    image: code.puffinoffset.com/matt/puffin-app:backend-latest
    container_name: puffin-backend
    ports:
      - "3801:3001"
    volumes:
      - puffin-data:/app/data
    environment:
      - NODE_ENV=production
      - PORT=3001
      - FRONTEND_URL=${FRONTEND_URL:-https://puffinoffset.com}
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
      - STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET}
      - WREN_API_TOKEN=${WREN_API_TOKEN}
      - WREN_DRY_RUN=${WREN_DRY_RUN:-false}
      - DATABASE_PATH=/app/data/orders.db
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT}
      - SMTP_SECURE=${SMTP_SECURE}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASSWORD=${SMTP_PASSWORD}
      - SMTP_FROM_NAME=${SMTP_FROM_NAME}
      - SMTP_FROM_EMAIL=${SMTP_FROM_EMAIL}
      - ADMIN_EMAIL=${ADMIN_EMAIL:-matt@puffinoffset.com}
    dns:
      - 8.8.8.8
      - 8.8.4.4
    networks:
      - puffin-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3001/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

networks:
  puffin-network:
    driver: bridge

volumes:
  puffin-data:
    driver: local
