services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: matchlivetv
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-matchlivetv_dev}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5

  mediamtx:
    image: bluenviron/mediamtx:latest
    ports:
      - "1935:1935"
      - "8554:8554"
      - "8888:8888"
      - "9997:9997"
    volumes:
      - ./mediamtx.yml:/mediamtx.yml:ro
      - ./slates:/slates:ro
      - recordings:/recordings
    command: /mediamtx.yml
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:9997/v3/config/global/get"]
      interval: 10s
      timeout: 5s
      retries: 3

  rails:
    build:
      context: ../backend
      dockerfile: Dockerfile
    command: bash -c "bundle exec rails db:prepare db:seed && bundle exec rails server -b 0.0.0.0 -p 3000"
    environment:
      RAILS_ENV: development
      DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD:-matchlivetv_dev}@postgres:5432/matchlivetv
      REDIS_URL: redis://redis:6379/0
      MEDIAMTX_API_URL: http://mediamtx:9997
      MEDIAMTX_RTMP_URL: rtmp://mediamtx:1935
      MEDIAMTX_WEBHOOK_SECRET: ${MEDIAMTX_WEBHOOK_SECRET:-mediamtx_webhook_dev_secret}
      RAILS_WEBHOOK_URL: http://rails:3000
      SECRET_KEY_BASE: ${SECRET_KEY_BASE:-dev_secret_key_base_change_me_32chars_min}
      JWT_SECRET: ${JWT_SECRET:-matchlivetv_jwt_dev_secret}
      YOUTUBE_CLIENT_ID: ${YOUTUBE_CLIENT_ID:-}
      YOUTUBE_CLIENT_SECRET: ${YOUTUBE_CLIENT_SECRET:-}
      YOUTUBE_REDIRECT_URI: ${YOUTUBE_REDIRECT_URI:-http://localhost:3000/api/v1/youtube/callback}
      RAILS_LOG_TO_STDOUT: "true"
    ports:
      - "3000:3000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      mediamtx:
        condition: service_started
    volumes:
      - ../backend:/app
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/up"]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 60s

  sidekiq:
    build:
      context: ../backend
      dockerfile: Dockerfile
    command: bundle exec sidekiq -C config/sidekiq.yml
    environment:
      RAILS_ENV: development
      DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD:-matchlivetv_dev}@postgres:5432/matchlivetv
      REDIS_URL: redis://redis:6379/0
      MEDIAMTX_API_URL: http://mediamtx:9997
      MEDIAMTX_WEBHOOK_SECRET: ${MEDIAMTX_WEBHOOK_SECRET:-mediamtx_webhook_dev_secret}
      RAILS_WEBHOOK_URL: http://rails:3000
      SECRET_KEY_BASE: ${SECRET_KEY_BASE:-dev_secret_key_base_change_me_32chars_min}
      JWT_SECRET: ${JWT_SECRET:-matchlivetv_jwt_dev_secret}
      YOUTUBE_CLIENT_ID: ${YOUTUBE_CLIENT_ID:-}
      YOUTUBE_CLIENT_SECRET: ${YOUTUBE_CLIENT_SECRET:-}
    depends_on:
      rails:
        condition: service_healthy
    volumes:
      - ../backend:/app

  nginx:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - rails

volumes:
  postgres_data:
  redis_data:
  recordings:
