name: ytdlp-ops
{% if service_role != 'management' %}
include:
  # This automatically includes the generated camoufox service definitions and dependencies.
  # It simplifies the docker-compose command, as you no longer need to specify both files with -f.
  # The file is generated by the config-generator service and will be created even if empty.
  - docker-compose.camoufox.yaml
{% endif %}

services:
  envoy:
    image: envoyproxy/envoy:v1.29-latest
    container_name: envoy-thrift-lb
    restart: unless-stopped
    volumes:
      # Mount the generated config file from the host
      - ./envoy.yaml:/etc/envoy/envoy.yaml:ro
    ports:
      # This is the single public port for all Thrift traffic
      - "${ENVOY_PORT:-9080}:${ENVOY_PORT:-9080}"
      # Expose the admin port for debugging
      - "${ENVOY_ADMIN_PORT:-9901}:${ENVOY_ADMIN_PORT:-9901}"
    networks:
      - proxynet
    # This service depends on ytdlp-ops-service, which in turn waits for camoufox.
    depends_on:
      - ytdlp-ops-service

  ytdlp-ops-service:
    image: pangramia/ytdlp-ops-server:latest # Don't comment out or remove, build is performed externally
    # container_name is omitted; Docker will use the service name for DNS.
    # This service depends on the camoufox-group service, which ensures all camoufox
    # instances are started before this service.
{% if service_role != 'management' %}
    depends_on:
      - camoufox-group
{% endif %}
    # Ports are no longer exposed directly. Envoy will connect to them on the internal network.
    env_file:
      - ./.env # Path is relative to the compose file
    volumes:
      - context-data:/app/context-data
      # Mount the generated endpoints file to make it available to the server
      - ./camoufox/camoufox_endpoints.json:/app/config/camoufox_endpoints.json:ro
      # Mount the plugin source code for live updates without rebuilding the image.
      # Assumes the plugin source is in a 'bgutil-ytdlp-pot-provider' directory
      # next to your docker-compose.yaml file.
      #- ./bgutil-ytdlp-pot-provider:/app/bgutil-ytdlp-pot-provider
    networks:
      - proxynet
    command:
      # --- Parameters for ALL service roles ---
      - "--port"
      - "${YTDLP_BASE_PORT:-9090}"
      - "--timeout"
      - "${YTDLP_TIMEOUT:-600}"
      - "--workers"
      - "${YTDLP_WORKERS:-3}"
      - "--verbose"
      - "--server-identity"
      - "${SERVER_IDENTITY:-ytdlp-ops-airflow-service}"
      - "--redis-host"
      - "${REDIS_HOST:-redis}"
      - "--redis-port"
      - "${REDIS_PORT:-6379}"
      - "--redis-password"
      - "${REDIS_PASSWORD}"
      - "--account-active-duration-min"
      - "${ACCOUNT_ACTIVE_DURATION_MIN:-30}"
      - "--account-cooldown-duration-min"
      - "${ACCOUNT_COOLDOWN_DURATION_MIN:-60}"
      - "--service-role"
      - "{{ service_role }}"

{% if service_role != 'management' %}
      # --- Parameters for worker/all-in-one roles ONLY ---
      - "--script-dir"
      - "/app"
      - "--context-dir"
      - "/app/context-data"
      - "--clean-context-dir"
      - "--clients"
      - "${YT_CLIENTS:-web,mweb,ios,android}"
      - "--proxies"
      - "{{ combined_proxies_str }}"
      - "--camoufox-endpoints-file"
      - "/app/config/camoufox_endpoints.json"
      - "--print-tokens"
      - "--stop-if-no-proxy"
{% endif %}
    restart: unless-stopped
    pull_policy: always

volumes:
  context-data:
    name: context-data

networks:
  proxynet:
    name: airflow_proxynet
    external: true
