# ============================================
# Docker Compose Development Override
# Updated: January 2026 - v4.0
# Port: 9000 (standard for Mawidi)
# Optimized for automatic updates and hot-reload
# ============================================

services:
  # Development service with auto-reload
  mawidi-dev:
    container_name: mawidi-nextjs-dev
    build:
      context: ./mawidi-site
      dockerfile: Dockerfile.dev
      cache_from:
        - type=local,src=/tmp/buildx-cache-dev
      cache_to:
        - type=local,dest=/tmp/buildx-cache-dev-new,mode=max
    image: mawidi-nextjs:dev
    ports:
      - "${DEV_PORT:-9000}:9000"
      - "9229:9229" # Node.js debugging port
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
      - NODE_ENV=development
      - NEXT_TELEMETRY_DISABLED=1
      # Port configuration - ALWAYS 9000
      - PORT=9000
      - HOSTNAME=0.0.0.0
      # Enable file watching
      - WATCHPACK_POLLING=true
      - CHOKIDAR_USEPOLLING=true
      - CHOKIDAR_INTERVAL=1000
      - WATCHPACK_POLLING_INTERVAL=1000
      # Development settings
      - NODE_OPTIONS=--max-old-space-size=4096 --enable-source-maps --inspect=0.0.0.0:9229
      - NEXT_PUBLIC_API_URL=${DEV_API_URL:-http://localhost:9000}
      - NEXT_PUBLIC_APP_URL=${DEV_APP_URL:-http://localhost:9000}
      - NEXT_PUBLIC_WHATSAPP_NUMBER=${NEXT_PUBLIC_WHATSAPP_NUMBER:-+447450018521}
      - NEXT_PUBLIC_GA_ID=${NEXT_PUBLIC_GA_ID}
      # Fast refresh
      - FAST_REFRESH=true
      - NEXT_WEBPACK_USE_SWC=true
      # Auth environment variables
      - NEXTAUTH_URL=${NEXTAUTH_URL:-http://localhost:9000}
      - NEXTAUTH_SECRET=${NEXTAUTH_SECRET}
      - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
      - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
      # Database connection
      - DATABASE_URL=postgresql://mawidi:${DB_PASSWORD:-mawidi_db_pass_2025}@mawidi-postgres:5432/mawidi_production
      # Redis connection
      - REDIS_URL=redis://:${REDIS_PASSWORD:-mawidi_redis_pass_2025}@mawidi-redis:6379
      - UPSTASH_REDIS_REST_URL=${UPSTASH_REDIS_REST_URL}
      - UPSTASH_REDIS_REST_TOKEN=${UPSTASH_REDIS_REST_TOKEN}
      # Supabase configuration
      - NEXT_PUBLIC_SUPABASE_URL=http://host.docker.internal:54321
      - NEXT_PUBLIC_SUPABASE_ANON_KEY=${NEXT_PUBLIC_SUPABASE_ANON_KEY}
      - SUPABASE_SERVICE_ROLE_KEY=${SUPABASE_SERVICE_ROLE_KEY}
      # reCAPTCHA configuration
      - NEXT_PUBLIC_RECAPTCHA_SITE_KEY=${NEXT_PUBLIC_RECAPTCHA_SITE_KEY}
      - RECAPTCHA_SECRET_KEY=${RECAPTCHA_SECRET_KEY}
      - RECAPTCHA_SCORE_THRESHOLD=${RECAPTCHA_SCORE_THRESHOLD:-0.5}
      # Stripe configuration (CRITICAL for billing dashboard)
      - NEXT_PUBLIC_STRIPE_PUBLIC_KEY=${NEXT_PUBLIC_STRIPE_PUBLIC_KEY}
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
      - STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET}
      # n8n Webhook configuration
      - N8N_WEBHOOK_URL=${N8N_WEBHOOK_URL}
      - N8N_WEBHOOK_AUTH_TOKEN=${N8N_WEBHOOK_AUTH_TOKEN}
      - N8N_WEBHOOK_API_KEY=${N8N_WEBHOOK_API_KEY}
      # SMTP configuration (MailHog for dev)
      - SMTP_HOST=mawidi-mailhog
      - SMTP_PORT=1025
      - SMTP_SECURE=false
      - SMTP_USER=
      - SMTP_PASS=
      - SMTP_FROM=noreply@mawidi.local
      - EMAIL_QUEUE_ENABLED=false
      # ElevenLabs Voice Agent
      - ELEVENLABS_API_KEY=${ELEVENLABS_API_KEY}
      - NEXT_PUBLIC_ELEVENLABS_AGENT_ID=${NEXT_PUBLIC_ELEVENLABS_AGENT_ID}
      # Encryption key for OAuth tokens
      - ENCRYPTION_KEY=${ENCRYPTION_KEY}
    volumes:
      # Mount source code for hot-reload
      - ./mawidi-site:/app:delegated
      # Mount Claude configuration and scripts
      - ./.claude:/app/.claude:ro
      # Mount docker watch script
      - ./docker-watch.sh:/usr/local/bin/docker-watch:ro
      # Mount docker socket for container management
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Preserve node_modules in container
      - /app/node_modules
      # Use named volume for .next cache
      - next-dev-cache:/app/.next
    restart: unless-stopped
    networks:
      - mawidi-network
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:9000/api/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    # Enable init to handle signals properly
    init: true
    # Allocate tty for better logs
    tty: true
    stdin_open: true

  # Optional: Development database
  postgres-dev:
    image: postgres:16-alpine
    container_name: mawidi-postgres-dev
    ports:
      - "${DB_PORT:-5432}:5432"
    environment:
      - POSTGRES_USER=${DB_USER:-mawidi}
      - POSTGRES_PASSWORD=${DB_PASSWORD:-mawidi_db_pass_2025}
      - POSTGRES_DB=${DB_NAME:-mawidi_production}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - postgres-dev-data:/var/lib/postgresql/data
    networks:
      - mawidi-network
    profiles:
      - db
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-mawidi}"]
      interval: 30s
      timeout: 10s
      retries: 5

  # Optional: Development Redis
  redis-dev:
    image: redis:7-alpine
    container_name: mawidi-redis-dev
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru --appendonly yes --requirepass ${REDIS_PASSWORD:-mawidi_redis_pass_2025}
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis-dev-data:/data
    networks:
      - mawidi-network
    profiles:
      - cache
    restart: unless-stopped
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "redis-cli -a ${REDIS_PASSWORD:-mawidi_redis_pass_2025} ping | grep -q PONG",
        ]
      interval: 30s
      timeout: 10s
      retries: 3

  # Optional: Adminer for database management
  adminer:
    image: adminer:latest
    container_name: mawidi-adminer
    ports:
      - "8080:8080"
    environment:
      - ADMINER_DEFAULT_SERVER=postgres-dev
      - ADMINER_DESIGN=dracula
    networks:
      - mawidi-network
    profiles:
      - db
    restart: unless-stopped
    depends_on:
      - postgres-dev

networks:
  mawidi-network:
    external: true
    name: mawidi-network

volumes:
  next-dev-cache:
    name: mawidi-next-dev-cache
  postgres-dev-data:
    name: mawidi-postgres-dev-data
  redis-dev-data:
    name: mawidi-redis-dev-data
