x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-17}
    environment:
      TZ: ${TZ:-UTC}
      POSTGRES_USER: ${POSTGRES_USER:-opencut}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
      POSTGRES_DB: ${POSTGRES_DB:-opencut}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - '${POSTGRES_PORT_OVERRIDE:-5432}:5432'
    healthcheck:
      test: [CMD-SHELL, 'pg_isready -U ${POSTGRES_USER:-opencut}']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${OPENCUT_DB_CPU_LIMIT:-2.0}
          memory: ${OPENCUT_DB_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${OPENCUT_DB_CPU_RESERVATION:-0.1}
          memory: ${OPENCUT_DB_MEMORY_RESERVATION:-256M}

  redis:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-7-alpine}
    ports:
      - '${REDIS_PORT_OVERRIDE:-6379}:6379'
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${OPENCUT_REDIS_CPU_LIMIT:-1.0}
          memory: ${OPENCUT_REDIS_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${OPENCUT_REDIS_CPU_RESERVATION:-0.1}
          memory: ${OPENCUT_REDIS_MEMORY_RESERVATION:-128M}

  serverless-redis-http:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}hiett/serverless-redis-http:${SERVERLESS_REDIS_HTTP_VERSION:-latest}
    ports:
      - '${SERVERLESS_REDIS_HTTP_PORT_OVERRIDE:-8079}:80'
    environment:
      SRH_MODE: env
      SRH_TOKEN: ${SERVERLESS_REDIS_HTTP_TOKEN:?SERVERLESS_REDIS_HTTP_TOKEN is required}
      SRH_CONNECTION_STRING: 'redis://redis:6379'
    depends_on:
      redis:
        condition: service_healthy
    healthcheck:
      test: [CMD-SHELL, 'wget --spider -q http://127.0.0.1:80 || exit 1']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${OPENCUT_SRH_CPU_LIMIT:-1.0}
          memory: ${OPENCUT_SRH_MEMORY_LIMIT:-256M}
        reservations:
          cpus: ${OPENCUT_SRH_CPU_RESERVATION:-0.1}
          memory: ${OPENCUT_SRH_MEMORY_RESERVATION:-64M}

  web:
    <<: *defaults
    image: ${OPENCUT_WEB_IMAGE:-opencut/web:latest}
    ports:
      - '${OPENCUT_WEB_PORT_OVERRIDE:-3100}:3000'
    environment:
      TZ: ${TZ:-UTC}
      NODE_ENV: production
      DATABASE_URL: postgresql://${POSTGRES_USER:-opencut}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:-opencut}
      BETTER_AUTH_URL: ${OPENCUT_BETTER_AUTH_URL:-http://localhost:3100}
      BETTER_AUTH_SECRET: ${OPENCUT_BETTER_AUTH_SECRET:?OPENCUT_BETTER_AUTH_SECRET is required}
      UPSTASH_REDIS_REST_URL: http://serverless-redis-http:80
      UPSTASH_REDIS_REST_TOKEN: ${SERVERLESS_REDIS_HTTP_TOKEN}
      FREESOUND_CLIENT_ID: ${OPENCUT_FREESOUND_CLIENT_ID:?OPENCUT_FREESOUND_CLIENT_ID is required}
      FREESOUND_API_KEY: ${OPENCUT_FREESOUND_API_KEY:?OPENCUT_FREESOUND_API_KEY is required}
      # Transcription (Optional - leave blank to disable auto-captions)
      CLOUDFLARE_ACCOUNT_ID: ${OPENCUT_CLOUDFLARE_ACCOUNT_ID:-}
      R2_ACCESS_KEY_ID: ${OPENCUT_R2_ACCESS_KEY_ID:-}
      R2_SECRET_ACCESS_KEY: ${OPENCUT_R2_SECRET_ACCESS_KEY:-}
      R2_BUCKET_NAME: ${OPENCUT_R2_BUCKET_NAME:-}
      MODAL_TRANSCRIPTION_URL: ${OPENCUT_MODAL_TRANSCRIPTION_URL:-}
    depends_on:
      db:
        condition: service_healthy
      serverless-redis-http:
        condition: service_healthy
    healthcheck:
      test: [CMD-SHELL, 'curl -f http://localhost:3000/api/health || exit 1']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${OPENCUT_WEB_CPU_LIMIT:-2.0}
          memory: ${OPENCUT_WEB_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${OPENCUT_WEB_CPU_RESERVATION:-0.1}
          memory: ${OPENCUT_WEB_MEMORY_RESERVATION:-512M}

volumes:
  postgres_data:
