version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:16-alpine
    container_name: racing-postgres
    environment:
      POSTGRES_USER: racing
      POSTGRES_PASSWORD: racing123
      POSTGRES_DB: racing_db
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U racing -d racing_db"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - racing-network

  # Redis for Queue and Cache
  redis:
    image: redis:7-alpine
    container_name: racing-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - racing-network

  # Jaeger for Distributed Tracing
  jaeger:
    image: jaegertracing/all-in-one:latest
    container_name: racing-jaeger
    environment:
      - COLLECTOR_OTLP_ENABLED=true
    ports:
      - "16686:16686"  # Jaeger UI
      - "4318:4318"    # OTLP HTTP receiver
      - "4317:4317"    # OTLP gRPC receiver
    networks:
      - racing-network

  # Prometheus for Metrics
  prometheus:
    image: prom/prometheus:latest
    container_name: racing-prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    ports:
      - "9091:9090"  # Changed to avoid conflicts
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus
    networks:
      - racing-network
    depends_on:
      - app

  # Grafana for Dashboards
  grafana:
    image: grafana/grafana:latest
    container_name: racing-grafana
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
      - POSTGRES_USER=racing
      - POSTGRES_PASSWORD=racing123
      - POSTGRES_DB=racing_db
    ports:
      - "3000:3000"
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
      - ./grafana/dashboards:/var/lib/grafana/dashboards:ro
    networks:
      - racing-network
    depends_on:
      - prometheus
      - postgres

  # Application (optional - can run locally instead)
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: racing-app
    environment:
      - NODE_ENV=development
      - DATABASE_URL=postgresql://racing:racing123@postgres:5432/racing_db
      - REDIS_URL=redis://redis:6379
      - TAB_API_BASE_URL=https://api.tab.co.nz
      - TAB_API_KEY=not-required
      - MORNING_SCRAPE_CRON=0 6 * * *
      - TIMEZONE=Pacific/Auckland
      - API_RATE_LIMIT_PER_MINUTE=100
      - API_RETRY_ATTEMPTS=3
      - API_RETRY_DELAY_MS=1000
      - ENABLE_HARNESS_RACING=true
      - ENABLE_THOROUGHBRED_RACING=true
      - ENABLE_AUS_RACING=true
      - ENABLE_NZ_RACING=true
      - LOG_LEVEL=info
      - OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318
      - OTEL_SERVICE_NAME=racing-scraper
      - OTEL_TRACES_SAMPLER=parentbased_traceidratio
      - OTEL_TRACES_SAMPLER_ARG=1.0
      - ENABLE_TRACING=true
      - ENABLE_METRICS=true
      - METRICS_PORT=9090
      - PROMETHEUS_ENDPOINT=/metrics
      - HEALTH_CHECK_PORT=8080
      - ENABLE_HEALTH_CHECKS=true
    ports:
      - "8080:8080"  # Health checks
      - "9090:9090"  # Metrics endpoint
    volumes:
      - .:/app
      - /app/node_modules
      - ./logs:/app/logs
    networks:
      - racing-network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: npm run dev

volumes:
  postgres_data:
  redis_data:
  prometheus_data:
  grafana_data:

networks:
  racing-network:
    driver: bridge
