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
      - ./alembic.ini:/app/alembic.ini
      - ./migrations:/app/migrations
      - /mnt/nas/app_data:/mnt/nas/app_data
    environment:
      PYTHONPATH: /app
      DATABASE_URL: ${MIGRATION_DATABASE_URL}
    command: ["bash", "-lc", "alembic upgrade head"]
    networks:
      - default        # Hogy lássa a saját hálózatát
      - shared_db_net  # Hogy lássa a KÖZPONTI adatbázist!
    restart: "no"

  # 2. BACKEND API
  service_finder_api:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: service_finder_api
    env_file: .env
    volumes:
      - ./backend:/app
      - ./alembic.ini:/app/alembic.ini
      - ./migrations:/app/migrations
    # Fontos: A 0.0.0.0 host kell, hogy a Proxy elérje!
    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  # <--- ITT KAPCSOLÓDIK A KÖZPONTHOZ
    restart: unless-stopped

  # 3. MINIO (Lokális marad a projekthez, de 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 (Gyorsítótár - Lokális marad)
  redis:
    image: redis:alpine
    container_name: service_finder_redis
    volumes:
      - /mnt/nas/app_data/redis_data:/data
    networks:
      - default
    restart: unless-stopped

  # 5. FRONTEND (Weboldal)
  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

# HÁLÓZATOK DEFINIÁLÁSA
networks:
  default:
    driver: bridge
  # Ez a kulcs! Megmondjuk neki, hogy használja a már létező központi hálót:
  shared_db_net:
    external: true