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

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
cd "$ROOT_DIR"

SANDBOX="$ROOT_DIR/sandbox/rho-join-admit"
JOIN_REPO="$SANDBOX/join-repo"
ADMIT_REPO="$SANDBOX/admit-repo"
OWNER_HOME="$SANDBOX/owner-rho-home"
COLLAB_HOME="$SANDBOX/collab-rho-home"
OWNER_ID="rho://id/github/rho-owner"
COLLAB_ID="rho://id/github/rho-collab"

setup_repo() {
  local repo="$1"
  mkdir -p "$repo"
  git -C "$repo" init >/dev/null
  git -C "$repo" config user.name "Rho Join Admit Test"
  git -C "$repo" config user.email "join-admit@example.test"
  git -C "$repo" config commit.gpgsign false
  git -C "$repo" switch -c main >/dev/null
  ./rho repo init \
    --root "$repo" \
    --repo-id rho://repo/local/join-admit \
    --owner "$OWNER_ID" >/dev/null
  RHO_HOME="$OWNER_HOME" ./rho id export \
    --identity "$OWNER_ID" \
    --out "$repo/rho/participants/rho-owner.yaml" >/dev/null
  RHO_HOME="$OWNER_HOME" ./rho repo sign-governance \
    --root "$repo" >/dev/null
  RHO_HOME="$OWNER_HOME" git -C "$repo" add .gitignore rho rho.yaml
  git -C "$repo" commit -m "Initialize Rho repo" >/dev/null
}

rm -rf "$SANDBOX"
mkdir -p "$SANDBOX"

RHO_HOME="$OWNER_HOME" ./rho id init \
  --github rho-owner \
  --display-name "Rho Owner" \
  --generate-ssh-key >/dev/null
RHO_HOME="$COLLAB_HOME" ./rho id init \
  --github rho-collab \
  --display-name "Rho Collaborator" \
  --generate-ssh-key >/dev/null
RHO_HOME="$COLLAB_HOME" ./rho id export \
  --identity "$COLLAB_ID" \
  --out "$SANDBOX/rho-collab-public.yaml" >/dev/null
RHO_HOME="$OWNER_HOME" ./rho id import "$SANDBOX/rho-collab-public.yaml" >/dev/null

setup_repo "$JOIN_REPO"
RHO_HOME="$COLLAB_HOME" ./rho repo join "$COLLAB_ID" \
  --root "$JOIN_REPO" \
  > "$SANDBOX/join-output.txt"
test "$(git -C "$JOIN_REPO" branch --show-current)" = "rho-collab/join-rho"
grep -q "join branch: rho-collab/join-rho" "$SANDBOX/join-output.txt"
grep -q "$COLLAB_ID" "$JOIN_REPO/rho/participants/rho-collab.yaml"
grep -q "$COLLAB_ID" "$JOIN_REPO/rho/membership.yaml"

setup_repo "$ADMIT_REPO"
RHO_HOME="$OWNER_HOME" ./rho repo admit "$COLLAB_ID" \
  --root "$ADMIT_REPO" \
  --rho-bin "$ROOT_DIR/rho" \
  > "$SANDBOX/admit-output.txt"
test "$(git -C "$ADMIT_REPO" branch --show-current)" = "review/rho-collab/admit-rho"
grep -q "admit branch: review/rho-collab/admit-rho" "$SANDBOX/admit-output.txt"
grep -q "rho repo doctor passed" "$SANDBOX/admit-output.txt"
grep -q "$COLLAB_ID" "$ADMIT_REPO/rho/participants/rho-collab.yaml"
grep -q "$COLLAB_ID" "$ADMIT_REPO/rho/membership.yaml"
grep -Fq "rho/messages/inbox/id/github/rho-collab/**" "$ADMIT_REPO/rho/policy/permissions.yaml"
grep -Fq "rho/messages/inbox/id/github/rho-collab/** filter=rho-crypt" "$ADMIT_REPO/.gitattributes"
grep -Fq "# >>> rho generated encrypted path filters >>>" "$ADMIT_REPO/.gitattributes"
test "$(grep -Fc "rho/messages/inbox/id/github/rho-collab/** filter=rho-crypt" "$ADMIT_REPO/.gitattributes")" = "1"
mkdir -p "$ADMIT_REPO/rho/messages/inbox/id/github/rho-collab"
RHO_HOME="$OWNER_HOME" ./rho repo doctor --root "$ADMIT_REPO" > "$SANDBOX/admit-doctor-canonical.txt"
if grep -q "migration: legacy inbox path" "$SANDBOX/admit-doctor-canonical.txt"; then
  echo "doctor reported migration for canonical inbox path" >&2
  exit 1
fi

echo "inspect sandbox: $SANDBOX"
find "$SANDBOX" \
  -path "*/.git" -prune -o \
  -maxdepth 7 -type f -print | sort
echo "rho join admit e2e passed"
