services:
  # 1. ADATBÁZIS MIGRÁCIÓ (Alembic)
  migrate:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_migrate
    env_file: .env
    volumes:
      - ./backend:/app
    environment:
      - PYTHONPATH=/app
    command: >
      bash -c "alembic upgrade head"
    networks:
      - default
      - shared_db_net
    restart: "no"

  # 2. BACKEND API (FastAPI)
  service_finder_api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_api
    env_file: .env
    ports:
      - "8000:8000"
    volumes:
      - ./backend:/app
      - /mnt/nas/app_data:/mnt/nas/app_data
      - ./static_previews:/app/static/previews
    environment:
      - PYTHONPATH=/app
    depends_on:
      migrate:
        condition: service_completed_successfully
      minio:
        condition: service_started
      redis:
        condition: service_started
    networks:
      - default
      - shared_db_net
    restart: unless-stopped

  # 3. MINIO (Object Storage)
  minio:
    image: minio/minio
    container_name: service_finder_minio
    env_file: .env
    command: server /data --console-address ":9001"
    volumes:
      - /mnt/nas/app_data/minio_data:/data
    networks:
      - default
    restart: unless-stopped

  # 4. REDIS (Cache & Queue)
  redis:
    image: redis:alpine
    container_name: service_finder_redis
    volumes:
      - /mnt/nas/app_data/redis_data:/data
    networks:
      - default
    restart: unless-stopped

  # 5. FRONTEND
  service_frontend:
    build:
      context: ./frontend
    container_name: service_finder_frontend
    env_file: .env
    ports:
      - "3001:80"
    networks:
      - default
    depends_on:
      service_finder_api:
        condition: service_started
    restart: unless-stopped

  # 6. KATALÓGUS ROBOT (Discovery)
  catalog_robot:
    build: ./backend
    command: python -u -m app.workers.catalog_robot
    deploy:
      replicas: 1
    volumes:
      - ./backend:/app
    env_file: .env
    depends_on:
      migrate:
        condition: service_completed_successfully
    networks:
      - default
      - shared_db_net
    restart: always

  # 7. SERVICE HUNTER (Web Scraping)
  service_hunter:
    build: ./backend
    container_name: service_finder_robot_hunter
    command: python -u -m app.workers.service_hunter
    volumes:
      - ./backend:/app
    env_file: .env
    depends_on:
      migrate:
        condition: service_completed_successfully
    networks:
      - default
      - shared_db_net
    restart: always

  # 8. n8n AUTOMATIZÁCIÓ
  n8n:
    image: n8nio/n8n:latest
    container_name: service_finder_n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    env_file: .env
    volumes:
      - ./n8n/data:/home/node/.n8n
    networks:
      - default
      - shared_db_net
    depends_on:
      - n8n_db

  n8n_db:
    image: postgres:15-alpine
    container_name: service_finder_n8n_db
    restart: unless-stopped
    env_file: .env
    volumes:
      - ./n8n/db_data:/var/lib/postgresql/data
    networks:
      - default

  # 9. BROWSERLESS
  browserless:
    image: browserless/chrome:latest
    container_name: service_finder_browserless
    restart: unless-stopped
    ports:
      - "3005:3000"
    networks:
      - default

  # 10. ROBOT 2.1 - RESEARCHER (Porszívó - Hálózati kutató)
  # Mivel I/O bound (netre vár), futtathatjuk több példányban (pl. 3 szálon)
  robot_researcher:
    build: ./backend
    command: python -u -m app.workers.researcher_v2_1
    deploy:
      replicas: 3
    volumes:
      - ./backend:/app
    env_file: .env
    depends_on:
      migrate:
        condition: service_completed_successfully
    networks:
      - default
      - shared_db_net
    restart: always

  # 11. ROBOT 2.2 - ALCHEMIST (Vegyész - GPU AI dúsító)
  # Ez használja a GPU-t, ebből általában 1 példány elég a VRAM miatt
  robot_alchemist:
    build: ./backend
    command: python -u -m app.workers.alchemist_v2_2
    deploy:
      replicas: 1
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    volumes:
      - ./backend:/app
    env_file: .env
    depends_on:
      migrate:
        condition: service_completed_successfully
      ollama:
        condition: service_started
    networks:
      - default
      - shared_db_net
    restart: always

  # 12. AI a szerveren :)
  ollama:
    image: ollama/ollama:latest
    container_name: service_finder_ollama
    restart: always
    volumes:
      - ./ollama_data:/root/.ollama
    ports:
      - "11434:11434"
    environment:
      - OLLAMA_KEEP_ALIVE=24h
      - OLLAMA_ORIGINS="*"
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    networks:
      - default
      - shared_db_net

  # 13. VIN AUDITOR
  vin_auditor:
    build: ./backend
    container_name: service_finder_vin_auditor
    command: python -u -m app.workers.vin_auditor
    restart: always
    env_file: .env
    depends_on:
      ollama:
        condition: service_started
    networks:
      - default
      - shared_db_net


networks:
  default:
    driver: bridge
  shared_db_net:
    external: true