version: '3.8'

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

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

  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    depends_on:
      - memcached-exporter
    networks:
      - app_network
      
  grafana:
    image: grafana/grafana
    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:15
    container_name: postgres_db
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_HOST_AUTH_METHOD: trust
    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

  backend:
      build:
        context: ./backend
        dockerfile: Dockerfile
      container_name: fastapi_backend
      working_dir: /app
      env_file:
        - ./backend/.env
      environment:
        - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres_db/${POSTGRES_DB}
        - 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"
        - "5678:5678"  # Debug port
      depends_on:
        db:
          condition: service_healthy
        memcached:
          condition: service_started
        clamav:
          condition: service_started
      networks:
        - app_network
      command: >
          sh -c "echo 'Waiting for services...' &&
          while ! nc -z db 5432; do sleep 1; done && echo 'Database ready' &&
          while ! nc -z memcached 11211; do sleep 1; done && echo 'Memcached ready' &&
          while ! nc -z clamav 3310; do sleep 1; done && echo 'ClamAV ready' &&
          echo 'Running migrations...' &&
          alembic upgrade head &&
          echo 'Starting application...' &&
          uvicorn main:app --host 0.0.0.0 --port 8000 --reload"

  clamav:
    image: clamav/clamav:stable
    container_name: clamav
    environment:
    - CLAMD_STARTUP_TIMEOUT=300
    volumes:
      - uploads:/app/public/uploads 
    ports:
      - "3310:3310"
    networks:
      - app_network

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: nuxt_frontend
    working_dir: /app
    env_file:
      - ./frontend/.env  # Frontend specific environment variables
    environment:
      # For browser requests (client-side)
      - NUXT_PUBLIC_API_BASE_URL=http://www.courierdepot.net:8000/api
      # For server-side requests (during SSR)
      - API_BASE_URL=http://backend:8000/api
      - NODE_ENV=development
    volumes:
      - ./frontend:/app
      - /app/node_modules
    ports:
      - "3000:3000"
    depends_on:
      - backend
    networks:
      - app_network
    command: sh -c "npm install --legacy-peer-deps --ignore-scripts && npm run dev"


volumes:
  postgres_data:
  grafana_data:
  uploads:

networks:
  app_network:
    driver: bridge