services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: s2yt
      POSTGRES_PASSWORD: s2yt
      POSTGRES_DB: s2yt
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U s2yt -d s2yt"]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  web:
    build: .
    restart: unless-stopped
    ports:
      - "${S2YT_PORT:-3000}:3000"
    env_file:
      - .env
    environment:
      S2YT_EDITION: selfhosted
      DATABASE_URL: postgresql://s2yt:s2yt@postgres:5432/s2yt
      REDIS_URL: redis://redis:6379
      UPLOAD_DIR: /app/uploads
      NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3000}
    volumes:
      - uploads_data:/app/uploads
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: >
      sh -c "npx prisma db push && node server.js"

  worker:
    build: .
    restart: unless-stopped
    env_file:
      - .env
    environment:
      S2YT_EDITION: selfhosted
      DATABASE_URL: postgresql://s2yt:s2yt@postgres:5432/s2yt
      REDIS_URL: redis://redis:6379
      UPLOAD_DIR: /app/uploads
    volumes:
      - uploads_data:/app/uploads
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: ["npx", "tsx", "worker/index.ts"]

volumes:
  postgres_data:
  redis_data:
  uploads_data:
