version: '3.8'

services:
  # پایگاه داده MySQL
  mysql:
    image: mysql:8.0
    container_name: telegram-bot-mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-rootpassword}
      MYSQL_DATABASE: ${DB_NAME:-telegram_bot}
      MYSQL_USER: ${DB_USER:-botuser}
      MYSQL_PASSWORD: ${DB_PASS:-botpassword}
    volumes:
      - mysql_data:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "3306:3306"
    networks:
      - bot-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  # Redis برای کش و صف
  redis:
    image: redis:7-alpine
    container_name: telegram-bot-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - bot-network
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      timeout: 5s
      retries: 3

  # برنامه FastAPI
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: telegram-bot-backend
    environment:
      - BOT_TOKEN=${BOT_TOKEN}
      - ROOT_CHATID=${ROOT_CHATID:-555566667777}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_USER=${DB_USER:-botuser}
      - DB_PASS=${DB_PASS:-botpassword}
      - DB_NAME=${DB_NAME:-telegram_bot}
      - REDIS_URL=redis://redis:6379/0
      - ADMIN_PANEL_SECRET=${ADMIN_PANEL_SECRET}
      - WEBHOOK_SECRET=${WEBHOOK_SECRET}
    ports:
      - "8000:8000"
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - bot-network
    volumes:
      - ./logs:/app/logs
    restart: unless-stopped

  # فرانت‌اند React
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: telegram-bot-frontend
    environment:
      - VITE_API_URL=http://backend:8000
    ports:
      - "3000:3000"
    depends_on:
      - backend
    networks:
      - bot-network
    restart: unless-stopped

  # Nginx برای پروکسی معکوس و SSL
  nginx:
    image: nginx:alpine
    container_name: telegram-bot-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/ssl:/etc/nginx/ssl
      - ./logs/nginx:/var/log/nginx
    depends_on:
      - backend
      - frontend
    networks:
      - bot-network
    restart: unless-stopped

  # مانیتورینگ (اختیاری)
  prometheus:
    image: prom/prometheus:latest
    container_name: telegram-bot-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    networks:
      - bot-network
    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'

  grafana:
    image: grafana/grafana:latest
    container_name: telegram-bot-grafana
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/dashboards:/etc/grafana/provisioning/dashboards
      - ./monitoring/datasources:/etc/grafana/provisioning/datasources
    networks:
      - bot-network
    depends_on:
      - prometheus

networks:
  bot-network:
    driver: bridge

volumes:
  mysql_data:
  redis_data:
  prometheus_data:
  grafana_data: