version: "3.9"

services:

  # ─── Torrent Scraper ──────────────────────────────────────────────────────
  scraper:
    build:
      context: ./scraper
      dockerfile: Dockerfile
    image: magnetio-scraper:latest
    container_name: magnetio_scraper
    restart: unless-stopped
    environment:
      - PORT=8080
      - REDIS_URI=redis://:${REDIS_PASSWORD:-magnetio-redis-secret}@redis:6379
      - CACHE_TTL_STREAMS=${CACHE_TTL_STREAMS:-86400}
      - LOG_LEVEL=${LOG_LEVEL:-info}
      - PREWARM_ENABLED=${PREWARM_ENABLED:-true}
      - PREWARM_SCHEDULE=${PREWARM_SCHEDULE:-0 3 * * *}
      - PREWARM_MOVIE_LIMIT=${PREWARM_MOVIE_LIMIT:-500}
      - PREWARM_SERIES_LIMIT=${PREWARM_SERIES_LIMIT:-200}
      - PREWARM_CACHE_TTL=${PREWARM_CACHE_TTL:-86400}
    depends_on:
      redis:
        condition: service_healthy
    networks:
      - magnetio_net
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
      interval: 30s
      timeout: 5s
      retries: 3

  # ─── Stremio Addon ────────────────────────────────────────────────────────
  addon:
    build:
      context: ./addon
      dockerfile: Dockerfile
    image: magnetio-addon:latest
    container_name: magnetio_addon
    restart: unless-stopped
    ports:
      - "${PORT:-7000}:7000"
    environment:
      - PORT=7000
      - REDIS_URI=redis://:${REDIS_PASSWORD:-magnetio-redis-secret}@redis:6379
      - SCRAPER_URL=http://scraper:8080
      - METRICS_USER=${METRICS_USER:-admin}
      - METRICS_PASSWORD=${METRICS_PASSWORD:-magnetio}
      - LOG_LEVEL=${LOG_LEVEL:-info}
      - ADDON_PUBLIC_URL=${ADDON_PUBLIC_URL:-}
      - OPENSUBTITLES_API_KEY=${OPENSUBTITLES_API_KEY:-}
      - OPENSUBTITLES_USERNAME=${OPENSUBTITLES_USERNAME:-}
      - OPENSUBTITLES_PASSWORD=${OPENSUBTITLES_PASSWORD:-}
      - OPENSUBTITLES_USER_AGENT=${OPENSUBTITLES_USER_AGENT:-Magnetio v1.0.0}
      - FFSUBSYNC_PATH=${FFSUBSYNC_PATH:-}
      - ALASS_PATH=${ALASS_PATH:-}
      - SUBTITLE_SYNC_MAX_OFFSET_SECONDS=${SUBTITLE_SYNC_MAX_OFFSET_SECONDS:-300}
      - ALASS_SPLIT_PENALTY=${ALASS_SPLIT_PENALTY:-10}
    depends_on:
      redis:
        condition: service_healthy
      scraper:
        condition: service_healthy
    networks:
      - magnetio_net
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:7000/health"]
      interval: 30s
      timeout: 5s
      retries: 3

  # ─── Redis Cache ──────────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: magnetio_redis
    restart: unless-stopped
    command: redis-server --save 60 1 --loglevel warning --maxmemory 512mb --maxmemory-policy allkeys-lru --requirepass ${REDIS_PASSWORD:-magnetio-redis-secret}
    volumes:
      - ${REDIS_DATA_PATH:-redis_data}:/data
    networks:
      - magnetio_net
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ─── Jackett (Optional) ───────────────────────────────────────────────────
  jackett:
    image: lscr.io/linuxserver/jackett:latest
    container_name: magnetio_jackett
    restart: unless-stopped
    profiles:
      - jackett
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=${TZ:-Etc/UTC}
      - AUTO_UPDATE=true
    volumes:
      - jackett_config:/config
      - jackett_downloads:/downloads
    ports:
      - "${JACKETT_PORT:-9117}:9117"
    networks:
      - magnetio_net

  # ─── Prowlarr (Optional) ─────────────────────────────────────────────────
  prowlarr:
    image: lscr.io/linuxserver/prowlarr:latest
    container_name: magnetio_prowlarr
    restart: unless-stopped
    profiles:
      - prowlarr
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=${TZ:-Etc/UTC}
    volumes:
      - prowlarr_config:/config
    ports:
      - "${PROWLARR_PORT:-9696}:9696"
    networks:
      - magnetio_net

volumes:
  redis_data:
  jackett_config:
  jackett_downloads:
  prowlarr_config:

networks:
  magnetio_net:
    driver: bridge
