version: '3.9'

services:

  nginx:
    image: nginx:1.25-alpine
    container_name: eclyss_nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/nginx/eclyss.conf:/etc/nginx/conf.d/default.conf:ro
      - ./public:/var/www/html/public:ro
      - ./storage:/var/www/html/storage:ro
      - nginx_logs:/var/log/nginx
    depends_on:
      - php
      - websocket
    restart: unless-stopped
    networks:
      - eclyss_net

  php:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    container_name: eclyss_php
    volumes:
      - .:/var/www/html
      - ./storage:/var/www/html/storage
    environment:
      APP_ENV:       ${APP_ENV:-production}
      APP_KEY:       ${APP_KEY}
      DB_HOST:       mysql
      DB_PORT:       3306
      DB_NAME:       ${DB_NAME:-eclyss}
      DB_USER:       ${DB_USER:-eclyss}
      DB_PASS:       ${DB_PASS}
      REDIS_HOST:    redis
      REDIS_PORT:    6379
      REDIS_PASS:    ${REDIS_PASS:-}
      JWT_SECRET:    ${JWT_SECRET}
      AI_SERVICE_URL: http://ai:8001
      AI_API_KEY:    ${AI_API_KEY}
      FCM_SERVER_KEY: ${FCM_SERVER_KEY:-}
      APNS_KEY_PATH:  ${APNS_KEY_PATH:-}
      STORAGE_PATH:  /var/www/html/storage
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - eclyss_net

  websocket:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    container_name: eclyss_ws
    command: php /var/www/html/public/ws.php
    ports:
      - "8080:8080"
    volumes:
      - .:/var/www/html
    environment:
      DB_HOST:    mysql
      DB_NAME:    ${DB_NAME:-eclyss}
      DB_USER:    ${DB_USER:-eclyss}
      DB_PASS:    ${DB_PASS}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASS: ${REDIS_PASS:-}
      JWT_SECRET: ${JWT_SECRET}
      WS_PORT:    8080
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - eclyss_net

  mysql:
    image: mysql:8.0
    container_name: eclyss_mysql
    environment:
      MYSQL_DATABASE:      ${DB_NAME:-eclyss}
      MYSQL_USER:          ${DB_USER:-eclyss}
      MYSQL_PASSWORD:      ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./migrations:/docker-entrypoint-initdb.d:ro
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --default-authentication-plugin=caching_sha2_password
      --innodb-buffer-pool-size=256M
      --max-connections=200
      --slow-query-log=1
      --slow-query-log-file=/var/log/mysql/slow.log
      --long-query-time=1
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASS}"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - eclyss_net

  redis:
    image: redis:7-alpine
    container_name: eclyss_redis
    command: >
      redis-server
      --appendonly yes
      --maxmemory 256mb
      --maxmemory-policy allkeys-lru
      --requirepass ${REDIS_PASS:-}
      --loglevel warning
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    restart: unless-stopped
    networks:
      - eclyss_net

  ai:
    build:
      context: ./ai-service
      dockerfile: Dockerfile
    container_name: eclyss_ai
    environment:
      AI_API_KEY:   ${AI_API_KEY}
      WORKERS:      ${AI_WORKERS:-2}
      MODEL_SIZE:   ${WHISPER_MODEL:-small}
    volumes:
      - ./ai-service/models:/app/models
    ports:
      - "127.0.0.1:8001:8001"
    deploy:
      resources:
        limits:
          memory: 3G
        reservations:
          memory: 1G
    restart: unless-stopped
    networks:
      - eclyss_net

volumes:
  mysql_data:
  redis_data:
  nginx_logs:

networks:
  eclyss_net:
    driver: bridge
