version: '3.8'

services:
  memcached:
    image: memcached:1.6.22-alpine
    container_name: cached
    ports:
      - "11211:11211"
    command: memcached -m 64 -v
    restart: unless-stopped
    networks:
      - app_network

  memcached-exporter:
    image: prom/memcached-exporter:v0.13.0
    command:
      - '--memcached.address=memcached:11211'
    ports:
      - "9150:9150"
    depends_on:
      - memcached
    networks:
      - app_network

  prometheus:
    image: prom/prometheus:v2.52.0
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    depends_on:
      - memcached-exporter
    networks:
      - app_network
      
  grafana:
    image: grafana/grafana:11.2.0
    ports:
      - "3001:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - app_network

  db:
    image: postgres:17.2
    container_name: postgres_db
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
      POSTGRES_DB: ${POSTGRES_DB:-appdb}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
      interval: 5s
      timeout: 10s
      retries: 5

  clamav:
    image: clamav/clamav:1.2.2
    container_name: clamav
    environment:
      - CLAMAV_NO_FRESHCLAMD=false
      - CLAMAV_NO_CLAMD=false
      - CLAMD_STARTUP_TIMEOUT=600
      - CLAMD_CONF_TCPADDRESS=0.0.0.0
      - CLAMD_CONF_TCPSOCKET=3310
      - CLAMD_CONF_STREAMMAXLENGTH=500M
      - CLAMD_CONF_MAXFILESIZE=500M
      - CLAMD_CONF_MAXFILES=5000
      - CLAMD_CONF_MAXSCANSIZE=500M
    volumes:
      - clamav_data:/var/lib/clamav
      - uploads:/scandir
    ports:
      - "3310:3310"
    networks:
      - app_network
    healthcheck:
      test: ["CMD-SHELL", "echo PING | nc localhost 3310 | grep -q PONG || exit 1"]
      interval: 30s
      timeout: 20s
      retries: 5
      start_period: 120s

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
      network: host
    container_name: fastapi_backend
    working_dir: /app
    env_file:
      - ./backend/.env
    environment:
      - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-changeme}@postgres_db/${POSTGRES_DB:-appdb}
      - MEMCACHED_URL=memcached:11211
      - DEBUG=true
      - CLAMD_HOST=clamav
      - CLAMD_PORT=3310
      - PYTHONPATH=/app
    volumes:
      - ./backend:/app
      - /app/__pycache__
      - uploads:/app/public/uploads 
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
      memcached:
        condition: service_started
      clamav:
        condition: service_healthy
    networks:
      - app_network
    command: >
      sh -c "echo 'Waiting for services...' &&
        until nc -z db 5432; do echo 'Waiting for database...'; sleep 2; done &&
        until nc -z memcached 11211; do echo 'Waiting for memcached...'; sleep 2; done &&
        until nc -z clamav 3310; do echo 'Waiting for ClamAV...'; sleep 2; done &&
        echo 'All services ready!' &&
        echo 'Running migrations...' &&
        alembic upgrade head &&
        echo 'Starting application...' &&
        uvicorn main:app --host 0.0.0.0 --port 8000 --reload"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  postgres_data:
  grafana_data:
  uploads:
  clamav_data:

networks:
  app_network:
    driver: bridge