# docker-compose.yml
version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:14-alpine
    container_name: telegram_bot_postgres
    environment:
      POSTGRES_DB: telegram_bot_master
      POSTGRES_USER: telegram_admin
      POSTGRES_PASSWORD: ${DB_PASSWORD:-SecurePass123!}
      POSTGRES_INITDB_ARGS: "--encoding=UTF8"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
    ports:
      - "5432:5432"
    networks:
      - telegram_bot_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U telegram_admin -d telegram_bot_master"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis for caching and sessions
  redis:
    image: redis:7-alpine
    container_name: telegram_bot_redis
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-RedisPass123!}
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    networks:
      - telegram_bot_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # RabbitMQ for message queue
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: telegram_bot_rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-telegram_bot}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:-RabbitPass123!}
      RABBITMQ_DEFAULT_VHOST: /
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - telegram_bot_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 30s
      timeout: 10s
      retries: 5

  # Backend API Service
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: telegram_bot_backend
    environment:
      - DATABASE_URL=postgresql://telegram_admin:${DB_PASSWORD:-SecurePass123!}@postgres:5432/telegram_bot_master
      - REDIS_URL=redis://:${REDIS_PASSWORD:-RedisPass123!}@redis:6379/0
      - RABBITMQ_URL=amqp://${RABBITMQ_USER:-telegram_bot}:${RABBITMQ_PASSWORD:-RabbitPass123!}@rabbitmq:5672/
      - SECRET_KEY=${SECRET_KEY:-your-secret-key-here-change-in-production}
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-your-jwt-secret-key-change-in-production}
      - ENVIRONMENT=${ENVIRONMENT:-development}
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    volumes:
      - ./backend:/app
      - backend_uploads:/app/uploads
      - backend_logs:/app/logs
    ports:
      - "8000:8000"
    networks:
      - telegram_bot_network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
    restart: unless-stopped
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload

  # Celery Worker for async tasks
  celery_worker:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: telegram_bot_celery
    environment:
      - DATABASE_URL=postgresql://telegram_admin:${DB_PASSWORD:-SecurePass123!}@postgres:5432/telegram_bot_master
      - REDIS_URL=redis://:${REDIS_PASSWORD:-RedisPass123!}@redis:6379/0
      - RABBITMQ_URL=amqp://${RABBITMQ_USER:-telegram_bot}:${RABBITMQ_PASSWORD:-RabbitPass123!}@rabbitmq:5672/
      - SECRET_KEY=${SECRET_KEY:-your-secret-key-here-change-in-production}
    volumes:
      - ./backend:/app
      - backend_uploads:/app/uploads
      - backend_logs:/app/logs
    networks:
      - telegram_bot_network
    depends_on:
      - backend
      - rabbitmq
      - redis
    restart: unless-stopped
    command: celery -A celery_app worker --loglevel=info

  # Celery Beat for scheduled tasks
  celery_beat:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: telegram_bot_celery_beat
    environment:
      - DATABASE_URL=postgresql://telegram_admin:${DB_PASSWORD:-SecurePass123!}@postgres:5432/telegram_bot_master
      - REDIS_URL=redis://:${REDIS_PASSWORD:-RedisPass123!}@redis:6379/0
      - RABBITMQ_URL=amqp://${RABBITMQ_USER:-telegram_bot}:${RABBITMQ_PASSWORD:-RabbitPass123!}@rabbitmq:5672/
    volumes:
      - ./backend:/app
    networks:
      - telegram_bot_network
    depends_on:
      - backend
      - rabbitmq
      - redis
    restart: unless-stopped
    command: celery -A celery_app beat --loglevel=info

  # Flower for monitoring Celery
  flower:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: telegram_bot_flower
    environment:
      - RABBITMQ_URL=amqp://${RABBITMQ_USER:-telegram_bot}:${RABBITMQ_PASSWORD:-RabbitPass123!}@rabbitmq:5672/
    ports:
      - "5555:5555"
    networks:
      - telegram_bot_network
    depends_on:
      - celery_worker
      - celery_beat
      - rabbitmq
    restart: unless-stopped
    command: celery -A celery_app flower --port=5555

  # Nginx for reverse proxy
  nginx:
    image: nginx:alpine
    container_name: telegram_bot_nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./frontend/owner-dashboard/dist:/usr/share/nginx/html/owner
      - ./frontend/admin-dashboard/dist:/usr/share/nginx/html/admin
      - nginx_logs:/var/log/nginx
    ports:
      - "80:80"
      - "443:443"
    networks:
      - telegram_bot_network
    depends_on:
      - backend
    restart: unless-stopped

  # Prometheus for monitoring
  prometheus:
    image: prom/prometheus:latest
    container_name: telegram_bot_prometheus
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    ports:
      - "9090:9090"
    networks:
      - telegram_bot_network
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'

  # Grafana for visualization
  grafana:
    image: grafana/grafana:latest
    container_name: telegram_bot_grafana
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
      - GF_INSTALL_PLUGINS=grafana-piechart-panel
    volumes:
      - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards
      - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources
      - grafana_data:/var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      - telegram_bot_network
    depends_on:
      - prometheus
    restart: unless-stopped

  # MinIO for object storage (APK files)
  minio:
    image: minio/minio:latest
    container_name: telegram_bot_minio
    environment:
      - MINIO_ROOT_USER=${MINIO_USER:-minioadmin}
      - MINIO_ROOT_PASSWORD=${MINIO_PASSWORD:-MinioPass123!}
    volumes:
      - minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    networks:
      - telegram_bot_network
    restart: unless-stopped
    command: server /data --console-address ":9001"

  # Create MinIO buckets
  minio_create_buckets:
    image: minio/mc:latest
    container_name: telegram_bot_minio_create_buckets
    depends_on:
      - minio
    networks:
      - telegram_bot_network
    entrypoint: >
      /bin/sh -c "
      until (/usr/bin/mc config host add minio http://minio:9000 ${MINIO_USER:-minioadmin} ${MINIO_PASSWORD:-MinioPass123!}) do echo '...waiting...' && sleep 1; done;
      /usr/bin/mc mb minio/apk-files || true;
      /usr/bin/mc policy set download minio/apk-files || true;
      echo 'Buckets created successfully';
      exit 0;
      "

  # Elasticsearch for logs
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.10.0
    container_name: telegram_bot_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:
      - telegram_bot_network
    restart: unless-stopped

  # Kibana for log visualization
  kibana:
    image: docker.elastic.co/kibana/kibana:8.10.0
    container_name: telegram_bot_kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
    ports:
      - "5601:5601"
    networks:
      - telegram_bot_network
    depends_on:
      - elasticsearch
    restart: unless-stopped

  # Logstash for log processing
  logstash:
    image: docker.elastic.co/logstash/logstash:8.10.0
    container_name: telegram_bot_logstash
    volumes:
      - ./monitoring/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
      - backend_logs:/var/log/backend:ro
    networks:
      - telegram_bot_network
    depends_on:
      - elasticsearch
    restart: unless-stopped

networks:
  telegram_bot_network:
    driver: bridge

volumes:
  postgres_data:
  redis_data:
  rabbitmq_data:
  backend_uploads:
  backend_logs:
  nginx_logs:
  prometheus_data:
  grafana_data:
  minio_data:
  elasticsearch_data: