version: "3.9"

services:
  journey-planner:
    image: ghcr.io/metroville/transit-planner:2.8.1
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      APP_ENV: production
      HTTP_PORT: "8080"
      LOG_LEVEL: info
      DATABASE_URL: postgresql://planner:${POSTGRES_PASSWORD}@database:5432/transit
      REDIS_URL: redis://cache:6379/0
      GTFS_DIRECTORY: /var/lib/transit/gtfs
      DEFAULT_TIMEZONE: America/Toronto
      DEFAULT_LOCALE: en-CA
      ROUTING_MAX_TRANSFERS: "4"
      ROUTING_WHEELCHAIR_ACCESSIBLE: "true"
      REALTIME_REFRESH_SECONDS: "30"
    volumes:
      - gtfs-data:/var/lib/transit/gtfs:ro
    depends_on:
      database:
        condition: service_healthy
      cache:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--spider", "http://localhost:8080/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
    networks:
      - transit-network

  gtfs-importer:
    image: ghcr.io/metroville/gtfs-importer:1.6.0
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql://planner:${POSTGRES_PASSWORD}@database:5432/transit
      GTFS_SOURCE_URL: https://data.metroville.ca/transit/gtfs.zip
      GTFS_OUTPUT_DIRECTORY: /var/lib/transit/gtfs
      IMPORT_SCHEDULE: "15 2 * * *"
      VALIDATE_FEED: "true"
      RETAIN_ARCHIVES_DAYS: "14"
    volumes:
      - gtfs-data:/var/lib/transit/gtfs
    depends_on:
      database:
        condition: service_healthy
    networks:
      - transit-network

  realtime-worker:
    image: ghcr.io/metroville/transit-realtime-worker:2.8.1
    restart: unless-stopped
    environment:
      DATABASE_URL: postgresql://planner:${POSTGRES_PASSWORD}@database:5432/transit
      REDIS_URL: redis://cache:6379/0
      VEHICLE_POSITIONS_URL: https://api.metroville.ca/gtfs-rt/vehicles
      TRIP_UPDATES_URL: https://api.metroville.ca/gtfs-rt/trips
      SERVICE_ALERTS_URL: https://api.metroville.ca/gtfs-rt/alerts
      POLL_INTERVAL_SECONDS: "15"
      REQUEST_TIMEOUT_SECONDS: "10"
      LOG_LEVEL: info
    depends_on:
      database:
        condition: service_healthy
      cache:
        condition: service_healthy
    networks:
      - transit-network

  database:
    image: postgis/postgis:16-3.4
    restart: unless-stopped
    environment:
      POSTGRES_DB: transit
      POSTGRES_USER: planner
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - database-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U planner -d transit"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - transit-network

  cache:
    image: redis:7.2-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes", "--maxmemory", "512mb", "--maxmemory-policy", "allkeys-lru"]
    volumes:
      - cache-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - transit-network

volumes:
  database-data:
  cache-data:
  gtfs-data:

networks:
  transit-network:
    driver: bridge