services:
  nextcloud-fpm:
    image: nextcloud:30-fpm-alpine
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      keydb:
        condition: service_healthy
      onlyoffice:
        condition: service_healthy
        required: false
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - REDIS_HOST=keydb
      - REDIS_HOST_PORT=6379
      - NEXTCLOUD_ADMIN_USER=${NC_ADMIN_USER:-admin}
      - NEXTCLOUD_ADMIN_PASSWORD=${NC_ADMIN_PASSWORD:-changeme}
      - NEXTCLOUD_TRUSTED_DOMAINS=${DOMAIN:-localhost} nextcloud
      - OBJECTSTORE_S3_BUCKET=nextcloud
      - OBJECTSTORE_S3_HOST=rustfs
      - OBJECTSTORE_S3_PORT=9000
      - OBJECTSTORE_S3_KEY=${RUSTFS_ACCESS_KEY}
      - OBJECTSTORE_S3_SECRET=${RUSTFS_SECRET_KEY}
      - OBJECTSTORE_S3_SSL=false
      - OBJECTSTORE_S3_USEPATH_STYLE=true
      - OVERWRITEHOST=${DOMAIN:-localhost}
      - OVERWRITEPROTOCOL=${NC_OVERWRITE_PROTOCOL:-http}
      - OVERWRITEWEBROOT=/cloud
      - OVERWRITECLIURL=${NC_PUBLIC_URL:-http://localhost/cloud}
      - TRUSTED_PROXIES=10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
      - NC_OIDC_CLIENT_ID=${NC_OIDC_CLIENT_ID:-ulti-nextcloud}
      - NC_OIDC_CLIENT_SECRET=${NC_OIDC_CLIENT_SECRET:-changeme}
      - NC_OIDC_DISCOVERY_URL=${NC_OIDC_DISCOVERY_URL:-http://nginx/auth/application/o/nextcloud/.well-known/openid-configuration}
      - ONLYOFFICE_ENABLED=${ONLYOFFICE_ENABLED:-false}
      - ONLYOFFICE_URL=${ONLYOFFICE_URL:-http://onlyoffice}
      - ONLYOFFICE_PUBLIC_URL=${ONLYOFFICE_PUBLIC_URL:-http://localhost/office}
      - ONLYOFFICE_JWT_SECRET=${ONLYOFFICE_JWT_SECRET:-}
      - NC_ONLYOFFICE_STORAGE_URL=${NC_ONLYOFFICE_STORAGE_URL:-http://nginx}
    volumes:
      - nextcloud_data:/var/www/html
      - ./nextcloud/init.sh:/docker-entrypoint-hooks.d/post-installation/init.sh:ro
      - ./nextcloud/configure-onlyoffice.sh:/docker-entrypoint-hooks.d/before-starting/50-configure-onlyoffice.sh:ro
    networks:
      - ulti-net
    healthcheck:
      test: ["CMD-SHELL", "php -r 'echo 1;' || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5

  nextcloud:
    image: nginx:alpine
    restart: unless-stopped
    depends_on:
      nextcloud-fpm:
        condition: service_healthy
    volumes:
      - ./nextcloud/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - nextcloud_data:/var/www/html:ro
    networks:
      - ulti-net
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1/status.php >/dev/null 2>&1 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s

  nextcloud-cron:
    image: nextcloud:30-fpm-alpine
    restart: unless-stopped
    depends_on:
      - nextcloud-fpm
    entrypoint: /cron.sh
    volumes:
      - nextcloud_data:/var/www/html
    networks:
      - ulti-net

volumes:
  nextcloud_data:
