services:
  # 1. MIGRÁCIÓ (Adatbázis szerkezet frissítése)
  migrate:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_migrate
    env_file: .env
    volumes:
      - ./backend:/app  # Ez tartalmazza az alembic.ini-t és a migrations mappát is!
    environment:
      PYTHONPATH: /app
      DATABASE_URL: ${MIGRATION_DATABASE_URL}
    command: ["bash", "-lc", "alembic upgrade head"]
    networks:
      - default
      - shared_db_net
    restart: "no"

  # 2. BACKEND API
  service_finder_api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_api
    env_file: .env
    volumes:
      - ./backend:/app
      - /mnt/nas/app_data:/mnt/nas/app_data     # Központi NAS elérés
      - ./static_previews:/app/static/previews  # Lokális SSD gyorsítótár
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips="*"
    ports:
      - "8000:8000"
    environment:
      PYTHONPATH: /app
      DATABASE_URL: ${DATABASE_URL}
      ALLOWED_ORIGINS: ${ALLOWED_ORIGINS}
      MINIO_ENDPOINT: ${MINIO_ENDPOINT}
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
    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 (NAS-ra ment)
  minio:
    image: minio/minio
    container_name: service_finder_minio
    env_file: .env
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
    volumes:
      - /mnt/nas/app_data/minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    networks:
      - default
    restart: unless-stopped

  # 4. REDIS (Lokális cache)
  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_finder_frontend:
    build:
      context: ./frontend
    container_name: service_finder_frontend
    env_file: .env
    ports:
      - "3001:80"
    environment:
      ALLOWED_ORIGINS: ${ALLOWED_ORIGINS}
    networks:
      - default
    depends_on:
      service_finder_api:
        condition: service_started
    restart: unless-stopped

  # Katalógus felderítő robot
  catalog_robot:
    build: ./backend
    image: service_finder-catalog_robot
    container_name: service_finder_robot_catalog
    command: python -m app.workers.catalog_robot
    volumes:
      - ./backend:/app
    env_file:
      - .env
    depends_on:
      migrate:
        condition: service_completed_successfully
    networks:
      - default
      - shared_db_net
    restart: always

  # Szerviz vadász robot (Robot 2.7)
  service_hunter:
    build: ./backend
    container_name: service_finder_robot_hunter
    command: python -m app.workers.service_hunter
    volumes:
      - ./backend:/app
      - ./backend/app/workers/local_services.csv:/app/app/workers/local_services.csv
    environment:
      - GOOGLE_API_KEY=${GOOGLE_API_KEY}
      # JAVÍTVA: shared-postgres lett a gépnév a 'db' helyett!
      - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@shared-postgres:5432/${POSTGRES_DB}
    env_file:
      - .env
    dns:
      - 8.8.8.8
      - 1.1.1.1
    depends_on:
      migrate:
        condition: service_completed_successfully
    networks:
      - default
      - shared_db_net
    restart: always

  # --- ÚJ: n8n AUTOMATIZÁCIÓ ---
  n8n:
    image: n8nio/n8n:latest
    container_name: service_finder_n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    env_file: .env
    environment:
      - N8N_HOST=0.0.0.0
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - N8N_SECURE_COOKIE=false  # <--- EZ JAVÍTJA A BELÉPÉSI HIBÁT!
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_DATABASE=n8n_internal
      - DB_POSTGRESDB_HOST=n8n_db
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_USER=n8n_admin
      - DB_POSTGRESDB_PASSWORD=${N8N_DB_PASSWORD}
    volumes:
      - ./n8n/data:/home/node/.n8n
    networks:
      - default
      - shared_db_net
    depends_on:
      - n8n_db

  # n8n belső meta-adatbázisa
  n8n_db:
    image: postgres:15-alpine
    container_name: service_finder_n8n_db
    restart: unless-stopped
    environment:
      - POSTGRES_USER=n8n_admin
      - POSTGRES_PASSWORD=${N8N_DB_PASSWORD}
      - POSTGRES_DB=n8n_internal
    volumes:
      - ./n8n/db_data:/var/lib/postgresql/data
    networks:
      - default

  # Browserless - A robot "szeme" (Központi 3005-ös porton)
  browserless:
    image: browserless/chrome:latest
    container_name: service_finder_browserless
    restart: unless-stopped
    ports:
      - "3005:3000"
    environment:
      - MAX_CONCURRENT_SESSIONS=10
    networks:
      - default

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