# Base local stack: single Neo4j Enterprise instance + floci S3 emulator + a
# node-agentless backup runner. Cluster topologies layer on via compose.cluster.yaml.
#
# Placement model (the "control placement of components" requirement):
#   - `neo4j` network: database members only.
#   - `ops` network: object storage and orchestration.
#   - `runner` bridges both — it is the only component that needs to reach the DB
#     backup port (6362) AND the object store. This mirrors the design's central
#     backup-runner host; nothing runs on the database nodes themselves.

name: neo4j-backup

networks:
  neo4j:
  ops:

volumes:
  neo4j-data:
  s3-data:
  # Runner scratch for --temp-path. In production this is a sized, dedicated volume
  # (instance-store/PVC) holding up to the largest full backup — several TB — never
  # the orchestrator's disk. Locally it is just a docker volume.
  scratch:

services:
  neo4j:
    image: neo4j:${NEO4J_VERSION}
    hostname: neo4j
    environment:
      NEO4J_ACCEPT_LICENSE_AGREEMENT: "eval"
      NEO4J_AUTH: "neo4j/${NEO4J_PASSWORD}"
      DB_PASSWORD: "${NEO4J_PASSWORD}"            # for healthcheck only
      # Expose the backup port to the runner (defaults to localhost otherwise).
      NEO4J_server_backup_enabled: "true"
      NEO4J_server_backup_listen__address: "0.0.0.0:6362"
      # seed-from-URI restore runs ON this server: it pulls the seed from object
      # storage itself, so the DB node needs the endpoint override + credentials.
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "${AWS_REGION}"
      AWS_ENDPOINT_URL_S3: "${AWS_ENDPOINT_URL_S3}"
      # Enable file:// seeding (#52 phase 4): validate restore cloud-agnostically from a
      # local artifact on /seed — the byte-identical .backup, no cloud fetch. CloudSeedProvider
      # keeps the s3:// path.
      NEO4J_dbms_databases_seed__from__uri__providers: "CloudSeedProvider,FileSeedProvider"
    ports:
      - "7474:7474"      # Neo4j Browser / HTTP
      - "7687:7687"      # Bolt
      - "6362:6362"      # backup port (so a k3d pod can reach it via host.k3d.internal)
    # On both networks: `neo4j` for DB traffic, `ops` because seed-from-URI restore
    # makes the node pull from object storage. A real cluster's members need the
    # same object-store egress.
    networks: [neo4j, ops]
    volumes:
      - neo4j-data:/data
      - ../.seed:/seed       # #52 phase 4: file:// restore validation (repo-root .seed on host)
    healthcheck:
      test: ["CMD-SHELL", "cypher-shell -u neo4j -p $${DB_PASSWORD} 'RETURN 1' || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 30
      start_period: 30s

  # A second, independent Neo4j (#115): the target of `restore --to-cluster`. Not started by
  # default: `docker compose --profile multi up -d neo4j-b`. Bolt on host port 7688; it reads
  # the same bucket, as any cluster serving the groups must.
  neo4j-b:
    image: neo4j:${NEO4J_VERSION}
    hostname: neo4j-b
    profiles: ["multi"]
    environment:
      NEO4J_ACCEPT_LICENSE_AGREEMENT: "eval"
      NEO4J_AUTH: "neo4j/${NEO4J_PASSWORD}"
      DB_PASSWORD: "${NEO4J_PASSWORD}"
      NEO4J_server_backup_enabled: "true"
      NEO4J_server_backup_listen__address: "0.0.0.0:6362"
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "${AWS_REGION}"
      AWS_ENDPOINT_URL_S3: "${AWS_ENDPOINT_URL_S3}"
    ports:
      - "7688:7687"
    networks: [neo4j, ops]
    healthcheck:
      test: ["CMD-SHELL", "cypher-shell -u neo4j -p $${DB_PASSWORD} 'RETURN 1' || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 30
      start_period: 30s

  # Local S3 + KMS emulator (floci). MinIO's images are no longer publicly pullable.
  # S3 API on 4566 inside the network; mapped to host port 9000 so host-side tools and
  # smokes keep using http://localhost:9000.
  s3:
    image: floci/floci:1.5.34
    ports:
      - "9000:4566"      # S3/KMS API (host 9000 -> floci 4566)
    networks: [ops]
    volumes:
      - s3-data:/app/data
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:4566/_localstack/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 30
      start_period: 5s

  # One-shot: ensure the KMS key alias and the backup bucket exist, then exit.
  # floci stores the bucket's default encryption but does not apply it to objects; writes
  # that send the SSE-KMS header themselves (BACKUP_UPLOAD=pipeline with S3_SSE) are encrypted.
  createbuckets:
    image: amazon/aws-cli:2.27.50
    depends_on:
      s3:
        condition: service_healthy
    networks: [ops]
    entrypoint: ["/bin/sh", "/init/create-bucket.sh"]
    volumes:
      - ./create-bucket.sh:/init/create-bucket.sh:ro
    environment:
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "${AWS_REGION}"
      AWS_ENDPOINT_URL: "http://s3:4566"
      BACKUP_BUCKET: "${BACKUP_BUCKET}"
      KMS_KEY_NAME: "${KMS_KEY_NAME}"

  # Node-agentless backup runner: same Neo4j image (for neo4j-admin), but it does not
  # run a server. It pulls backups over 6362 and writes them to object storage.
  runner:
    image: neo4j:${NEO4J_VERSION}
    hostname: runner
    entrypoint: ["sleep", "infinity"]
    depends_on:
      neo4j:
        condition: service_healthy
      createbuckets:
        condition: service_completed_successfully
    networks: [neo4j, ops]
    environment:
      NEO4J_ACCEPT_LICENSE_AGREEMENT: "eval"
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "${AWS_REGION}"
      AWS_ENDPOINT_URL_S3: "${AWS_ENDPOINT_URL_S3}"  # local S3 endpoint override (AWS SDK v2)
      SCRATCH_PATH: "${SCRATCH_PATH}"
      HEAP_SIZE: "${RUNNER_HEAP_SIZE}"               # caps neo4j-admin JVM heap (verify var)
    volumes:
      - scratch:/scratch                              # --temp-path staging (sized for fulls)

  # On-demand AWS CLI for inspecting/resolving artifacts. Not started by `up`.
  awscli:
    image: amazon/aws-cli:2.27.50
    profiles: ["tools"]
    networks: [ops]
    environment:
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
      AWS_REGION: "${AWS_REGION}"
      AWS_ENDPOINT_URL: "http://s3:4566"
      BACKUP_BUCKET: "${BACKUP_BUCKET}"

  # Azure Blob emulator (#52) — validates the Azure ObjectStore backend / floci-style local
  # Azure. Not started by default (profile). `docker compose --profile azure up -d azurite`.
  # skipApiVersionCheck: the azure-storage-blob SDK is ahead of Azurite's supported API version.
  azurite:
    image: mcr.microsoft.com/azure-storage/azurite
    profiles: ["azure"]
    command: ["azurite-blob", "--blobHost", "0.0.0.0", "--skipApiVersionCheck"]
    ports: ["10000:10000"]
    networks: [ops]

  # GCS emulator (#52) — validates the GCS ObjectStore backend / floci-style local GCP.
  # Profile. `docker compose --profile gcp up -d fake-gcs`; set STORAGE_EMULATOR_HOST.
  fake-gcs:
    image: fsouza/fake-gcs-server
    profiles: ["gcp"]
    command: ["-scheme", "http", "-public-host", "localhost:4443", "-port", "4443"]
    ports: ["4443:4443"]
    networks: [ops]
