#!/usr/bin/env bash
set -euo pipefail

repo_root="${REPO_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
cd "${repo_root}"

fail() {
  printf 'content-store tree contract: %s\n' "$1" >&2
  exit 1
}

active_contract=(
  internal/api/content
  internal/api/dto/content_store.go
  internal/resource/server/content_payload.go
  internal/resource/server/content_store_runtime.go
  internal/router/resource_routes.go
)

if grep -R -n -E 'DocumentPayloadBackend|ModelDocument|json:"model"' "${active_contract[@]}"; then
  fail "document/model selection leaked into the active API or runtime contract"
fi

grep -q 'func NewTree(' internal/service/content/service.go || fail "tree-only service constructor is missing"
grep -q 'NewGitTreeBackend(root)' internal/resource/server/content_payload.go || fail "runtime payload backend is not internal Git tree storage"
grep -q 'openContentTreePayload(cfg.FilesystemRoot)' internal/resource/server/content_store_runtime.go || fail "enabled runtime does not open the tree payload backend"
if grep -q 'NewFilesystemTreeBackend\|NewDocumentBackend' internal/resource/server/content_payload.go; then
  fail "runtime opens a non-tree or document payload backend"
fi

grep -q 'boolEnvDefault("MESHX_CONTENT_STORE_ENABLED", false)' internal/config/content_store_config.go || fail "single rollout switch is not default-off"
if grep -R -n --exclude='*_test.go' -E 'MESHX_CONTENT_STORE_TEST_|ManualTestWorkspaceID|ManualTestBindingKey|manual_test_tree' \
  .env.example docker-compose*.yml internal/config internal/resource/server specs/architecture; then
  fail "deployable manual-test binding configuration or runtime wiring remains"
fi
if grep -n -E 'NewNativeBindingPolicy|NewScopedTreeGCWorker|content-store-test-gc|WithContentStore\(|apicontentstore.RegisterRoutes' \
  internal/resource/server/server.go internal/resource/server/content_store_runtime.go internal/router/resource_routes.go; then
  fail "foundation runtime registers a generic content capability or maintenance owner"
fi
grep -q 'mux.Handle("/api/v1/content-store", http.NotFoundHandler())' internal/router/resource_routes.go || fail "dormant Content Store namespace can fall through to AGS"

# The dormant tree-platform stage has no startup/request-path backfill or
# mapping worker.
# The only available migration operations require an exact repository/content
# target and carry hard file, byte, batch, concurrency, and time bounds.
if grep -R -n -E 'ImportRepository\(|ProtectImportedVersions\(|VerifyRepository\(' \
  internal/api internal/router internal/resource/server --include='*.go'; then
  fail "migration/backfill work is reachable from startup or request paths"
fi
grep -q 'MaxGitMigrationProtectBatch = 100' internal/contentpayload/git_migration.go || fail "migration mapping batch bound is missing"
grep -q 'MaxGitMigrationConcurrency  = 4' internal/contentpayload/git_migration.go || fail "migration concurrency bound is missing"
grep -q 'DefaultGitMigrationMaxFiles = 1_000_000' internal/contentpayload/git_migration.go || fail "migration file bound is missing"
grep -q 'DefaultGitMigrationMaxBytes = int64(64 << 30)' internal/contentpayload/git_migration.go || fail "migration byte bound is missing"
grep -q 'withDefaultGitMigrationTimeout' internal/contentpayload/git_migration.go || fail "migration time bound is missing"
if grep -R -n -E 'New(Tree|ScopedTree)GCWorker\(|contentGCWorker\.RunOnce\(' internal/resource/server --include='*.go'; then
  fail "dormant-stage content maintenance is started"
fi
grep -q 'MaxGCLeaseBatch = 100' internal/store/content/gc.go || fail "GC lease batch bound is missing"
grep -q 'LeaseDueStagedPayloadGCForBinding' internal/store/content/gc.go || fail "GC lease query is not exact-binding scoped"

# This executable inventory is the dormant-stage diagnostic. It inspects
# reviewed source only and cannot enumerate workspaces or payload roots.
if grep -R -n -E 'List(All)?Workspaces|WalkDir|ReadDir' \
  internal/service/content internal/store/content --include='*.go'; then
  fail "content maintenance can enumerate a workspace or payload root"
fi

printf 'content-store tree contract: ok\n'
