services:
  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:?MYSQL_ROOT_PASSWORD is required}"
      MYSQL_DATABASE: "${MYSQL_DATABASE:-wain_db}"
      MYSQL_USER: "${MYSQL_USER:-wain_user}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD:?MYSQL_PASSWORD is required}"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD} --silent"]
      interval: 15s
      timeout: 10s
      retries: 20
      start_period: 300s

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_started
    expose:
      - "3000"
    healthcheck:
      test: ["CMD-SHELL", "node -e \"require('http').get('http://127.0.0.1:3000/', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));\""]
      interval: 15s
      timeout: 10s
      retries: 10
      start_period: 300s
    environment:
      NODE_ENV: production
      PORT: "3000"
      DATABASE_URL: "mysql://${MYSQL_USER:-wain_user}:${MYSQL_PASSWORD:?MYSQL_PASSWORD is required}@mysql:3306/${MYSQL_DATABASE:-wain_db}"
      JWT_SECRET: "${JWT_SECRET:?JWT_SECRET is required}"
      RECAPTCHA_SECRET: "${RECAPTCHA_SECRET:-6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe}"
      WA_SESSION_DIR: /app/wa-sessions
      WA_LOG_LEVEL: "${WA_LOG_LEVEL:-info}"
    volumes:
      - uploads_data:/app/uploads
      - wa_sessions:/app/wa-sessions
    command: >
      sh -c "node src/app.js"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: "${VITE_API_URL:-/api}"
    restart: unless-stopped
    depends_on:
      backend:
        condition: service_started
    expose:
      - "80"
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1/ || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s

volumes:
  mysql_data:
  uploads_data:
  wa_sessions:
