version: '3.8'

services:
  # Nginx Load Balancer
  nginx:
    image: nginx:alpine
    container_name: aladin-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/sites:/etc/nginx/conf.d
      - ./docker/ssl:/etc/nginx/ssl
      - ./backend/public:/var/www/html/backend/public
      - ./frontend/dist:/var/www/html/frontend
    depends_on:
      - backend
      - frontend
    networks:
      - aladin-network
    restart: unless-stopped

  # Laravel Backend
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: aladin-backend
    volumes:
      - ./backend:/var/www/html
      - ./docker/php/php.ini:/usr/local/etc/php/php.ini
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
      - APP_KEY=${APP_KEY}
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - MAIL_MAILER=smtp
      - MAIL_HOST=${MAIL_HOST}
      - MAIL_PORT=${MAIL_PORT}
      - MAIL_USERNAME=${MAIL_USERNAME}
      - MAIL_PASSWORD=${MAIL_PASSWORD}
      - STRIPE_KEY=${STRIPE_KEY}
      - STRIPE_SECRET=${STRIPE_SECRET}
      - PAYPAL_CLIENT_ID=${PAYPAL_CLIENT_ID}
      - PAYPAL_CLIENT_SECRET=${PAYPAL_CLIENT_SECRET}
    depends_on:
      - mysql
      - redis
    networks:
      - aladin-network
    restart: unless-stopped

  # React Frontend
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: aladin-frontend
    volumes:
      - ./frontend/dist:/usr/share/nginx/html
    networks:
      - aladin-network
    restart: unless-stopped

  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: aladin-mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=${DB_DATABASE}
      - MYSQL_USER=${DB_USERNAME}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
    ports:
      - "3306:3306"
    networks:
      - aladin-network
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password

  # Redis Cache
  redis:
    image: redis:alpine
    container_name: aladin-redis
    volumes:
      - redis_data:/data
      - ./docker/redis/redis.conf:/usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
    networks:
      - aladin-network
    restart: unless-stopped
    command: redis-server /usr/local/etc/redis/redis.conf

  # Elasticsearch for Search
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.10.0
    container_name: aladin-elasticsearch
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
    networks:
      - aladin-network
    restart: unless-stopped

  # RabbitMQ Message Queue
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: aladin-rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_USER}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - aladin-network
    restart: unless-stopped

  # Laravel Queue Worker
  queue-worker:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: aladin-queue-worker
    volumes:
      - ./backend:/var/www/html
    environment:
      - APP_ENV=production
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - QUEUE_CONNECTION=redis
    depends_on:
      - mysql
      - redis
    networks:
      - aladin-network
    restart: unless-stopped
    command: php artisan queue:work --sleep=3 --tries=3 --max-time=3600

  # Laravel Scheduler
  scheduler:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: aladin-scheduler
    volumes:
      - ./backend:/var/www/html
    environment:
      - APP_ENV=production
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
    depends_on:
      - mysql
    networks:
      - aladin-network
    restart: unless-stopped
    command: >
      sh -c "while true; do
        php artisan schedule:run --verbose --no-interaction &
        sleep 60
      done"

  # Monitoring - Prometheus
  prometheus:
    image: prom/prometheus:latest
    container_name: aladin-prometheus
    volumes:
      - ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    ports:
      - "9090:9090"
    networks:
      - aladin-network
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'

  # Monitoring - Grafana
  grafana:
    image: grafana/grafana:latest
    container_name: aladin-grafana
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards
      - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources
    ports:
      - "3000:3000"
    networks:
      - aladin-network
    restart: unless-stopped

  # Log Management - ELK Stack
  elasticsearch-logs:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.10.0
    container_name: aladin-elasticsearch-logs
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - elasticsearch_logs_data:/usr/share/elasticsearch/data
    ports:
      - "9201:9200"
    networks:
      - aladin-network
    restart: unless-stopped

  logstash:
    image: docker.elastic.co/logstash/logstash:8.10.0
    container_name: aladin-logstash
    volumes:
      - ./monitoring/logstash/pipeline:/usr/share/logstash/pipeline
      - ./monitoring/logstash/config:/usr/share/logstash/config
    ports:
      - "5044:5044"
    networks:
      - aladin-network
    depends_on:
      - elasticsearch-logs
    restart: unless-stopped

  kibana:
    image: docker.elastic.co/kibana/kibana:8.10.0
    container_name: aladin-kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch-logs:9200
    ports:
      - "5601:5601"
    networks:
      - aladin-network
    depends_on:
      - elasticsearch-logs
    restart: unless-stopped

  # Backup Service
  backup:
    image: alpine:latest
    container_name: aladin-backup
    volumes:
      - mysql_data:/backup/mysql
      - redis_data:/backup/redis
      - ./backups:/backups
      - ./docker/backup/backup.sh:/backup.sh
    environment:
      - DB_HOST=mysql
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
      - BACKUP_RETENTION_DAYS=30
      - S3_BUCKET=${BACKUP_S3_BUCKET}
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
    networks:
      - aladin-network
    restart: unless-stopped
    command: >
      sh -c "apk add --no-cache mysql-client aws-cli &&
             chmod +x /backup.sh &&
             while true; do
               /backup.sh
               sleep 86400
             done"

  # SSL Certificate Management
  certbot:
    image: certbot/certbot
    container_name: aladin-certbot
    volumes:
      - ./docker/ssl:/etc/letsencrypt
      - ./docker/ssl-challenge:/var/www/certbot
    networks:
      - aladin-network
    restart: "no"
    command: >
      sh -c "while true; do
        certbot renew --webroot --webroot-path=/var/www/certbot --quiet
        sleep 86400
      done"

  # Health Check Service
  healthcheck:
    image: alpine:latest
    container_name: aladin-healthcheck
    volumes:
      - ./docker/healthcheck/check.sh:/check.sh
    environment:
      - BACKEND_URL=http://backend:9000
      - FRONTEND_URL=http://frontend:80
      - MYSQL_HOST=mysql
      - REDIS_HOST=redis
      - ELASTICSEARCH_HOST=elasticsearch
    networks:
      - aladin-network
    restart: unless-stopped
    command: >
      sh -c "apk add --no-cache curl mysql-client redis &&
             chmod +x /check.sh &&
             while true; do
               /check.sh
               sleep 300
             done"

networks:
  aladin-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

volumes:
  mysql_data:
    driver: local
  redis_data:
    driver: local
  elasticsearch_data:
    driver: local
  elasticsearch_logs_data:
    driver: local
  rabbitmq_data:
    driver: local
  prometheus_data:
    driver: local
  grafana_data:
    driver: local
