services:
  postgres:
    image: postgres:17-alpine
    container_name: neda_postgres
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: always
    ports:
      - "5432:5432"
    networks:
      - internal
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}" ]
      interval: 5s
      timeout: 5s
      retries: 3

  redis:
    image: redis:7-alpine
    container_name: neda_redis
    volumes:
      - redis_data:/data
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    ports:
      - "6379:6379"
    networks:
      - internal
    healthcheck:
      test: [ "CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping" ]
      interval: 5s
      timeout: 3s
      retries: 5

  livekit:
    image: livekit/livekit-server
    container_name: neda_livekit
    ports:
      - "7780:7880"
      - "7781:7881"
      - "51000-51100:51000-51100/udp"
    env_file:
      - .env
    volumes:
      - ./livekit.yaml:/etc/livekit/livekit.yaml
    command: [ "--config", "/etc/livekit/livekit.yaml", "--keys", "${LIVEKIT_API_KEY}: ${LIVEKIT_API_SECRET}" ]
    restart: always
    networks:
      - public
      - internal
    healthcheck:
      test: [ "CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:7880/health" ] # بررسی سلامت LiveKit
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 10s

  pg_backup:
    image: prodrigestivill/postgres-backup-local
    container_name: neda_pg_backup
    restart: always
    profiles:
      - "prod"
    volumes:
      - ./backups:/backups
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      SCHEDULE: '@daily'
      BACKUP_KEEP_DAYS: 7
    networks:
      - internal
    depends_on:
      postgres:
        condition: service_healthy

  api:
    build: .
    container_name: neda_api
    ports:
      - "8000:8000"
    volumes:
      - "./:/app"
    env_file:
      - .env
    networks:
      - public
      - internal
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      livekit:
        condition: service_started
    restart: always

networks:
  public:
    driver: bridge
  internal:
    driver: bridge
    # internal: true

volumes:
  postgres_data:
  redis_data:
  pgadmin_data:
