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

WORKROOT="${WORKROOT:-/home/intel145/websites/dev}"
REPOROOT="${REPOROOT:-/home/intel145/repos}"

msg(){ printf '%s\n' "$*"; }
die(){ printf 'ERROR: %s\n' "$*" >&2; exit 1; }
ask(){ local v; read -r -p "$1 [$2]: " v || true; printf '%s\n' "${v:-$2}"; }
confirm(){ local p="$1" t="$2"; [ -e "$p" ] || die "$t not found: $p"; }

mkdir -p "$WORKROOT/docs"

cat > "$WORKROOT/git.master.menu.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

WORKROOT="${WORKROOT:-/home/intel145/websites/dev}"
REPOROOT="${REPOROOT:-/home/intel145/repos}"

msg(){ printf '%s\n' "$*"; }
die(){ printf 'ERROR: %s\n' "$*" >&2; exit 1; }
ask(){ local v; read -r -p "$1 [$2]: " v || true; printf '%s\n' "${v:-$2}"; }

create_repo_pair() {
  local category project version repo_dir work_dir branch
  category="$(ask 'Category' 'games')"
  project="$(ask 'Project name' 'hangman')"
  version="$(ask 'Version branch' 'v2')"
  branch="$version"

  repo_dir="$REPOROOT/$category/$project/$project.repository"
  work_dir="$WORKROOT/$category/$project/$project.test"

  msg "Repository root: $REPOROOT"
  msg "Worktree root:   $WORKROOT"
  msg "Category:        $category"
  msg "Project:         $project"
  msg "Version/branch:  $branch"
  msg "Bare repo:       $repo_dir"
  msg "Worktree:        $work_dir"

  mkdir -p "$(dirname "$repo_dir")" "$(dirname "$work_dir")"

  if [ ! -d "$repo_dir" ]; then
    git init --bare "$repo_dir" >/dev/null
    msg "Created bare repository."
  else
    git -C "$repo_dir" rev-parse --git-dir >/dev/null 2>&1 || die "Existing path is not a Git repository: $repo_dir"
    [ "$(git -C "$repo_dir" rev-parse --is-bare-repository)" = "true" ] || die "Existing repo is not bare: $repo_dir"
    msg "Bare repository already exists."
  fi

  if [ ! -e "$work_dir" ]; then
    if git -C "$repo_dir" show-ref --verify --quiet "refs/heads/$branch"; then
      git --git-dir="$repo_dir" worktree add "$work_dir" "$branch" >/dev/null
    else
      git --git-dir="$repo_dir" worktree add -b "$branch" "$work_dir" >/dev/null
    fi
    msg "Created worktree."
  else
    msg "Worktree path already exists; verifying registration."
    git -C "$repo_dir" worktree list --porcelain | grep -Fq "worktree $work_dir" || die "Path exists but is not a registered worktree: $work_dir"
  fi

  if git -C "$repo_dir" remote | grep -qx origin; then
    git -C "$repo_dir" remote set-url origin "file://$repo_dir"
  else
    git -C "$repo_dir" remote add origin "file://$repo_dir"
  fi

  msg "Remote origin set to: file://$repo_dir"
  msg "Edit here: cd $work_dir"
}

verify_repo() {
  local repo="$1"
  [ -e "$repo" ] || die "Path not found: $repo"
  git -C "$repo" rev-parse --git-dir >/dev/null 2>&1 || die "Not a Git repository: $repo"
  msg "Path: $repo"
  msg "Type: $( [ "$(git -C "$repo" rev-parse --is-bare-repository)" = true ] && echo bare || echo normal )"
  msg "Branch: $(git -C "$repo" branch --show-current 2>/dev/null || echo detached)"
  msg "Remotes:"
  if git -C "$repo" remote | grep -q .; then git -C "$repo" remote -v; else msg "  none"; fi
  msg
  msg "Worktrees:"
  git -C "$repo" worktree list --porcelain || true
  msg
  msg "FSCK:"
  git -C "$repo" fsck --full || true
}

view_docs() {
  local doc="$WORKROOT/docs/RECOMMENDATIONS.md"
  [ -f "$doc" ] || die "Documentation not found: $doc"
  if command -v less >/dev/null 2>&1; then less "$doc"; else cat "$doc"; fi
}

show_status() {
  local path
  path="$(ask 'Path to repo or worktree' "$WORKROOT")"
  [ -e "$path" ] || die "Path not found: $path"
  git -C "$path" rev-parse --git-dir >/dev/null 2>&1 || die "Not a Git repository: $path"
  git -C "$path" status --short --branch || true
}

while true; do
  printf '\nGit Workflow Menu\n'
  printf '1) Create repo pair\n'
  printf '2) Verify repository\n'
  printf '3) View recommendations\n'
  printf '4) Show status for a path\n'
  printf '5) Exit\n'
  choice="$(ask 'Choose' '1')"
  case "$choice" in
    1) create_repo_pair ;;
    2) verify_repo "$(ask 'Repository path' "$REPOROOT")" ;;
    3) view_docs ;;
    4) show_status ;;
    5) exit 0 ;;
    *) msg 'Invalid choice.' ;;
  esac
done
EOF

cat > "$WORKROOT/git.verify.repository.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
repo="${1:-/home/intel145/repos}"
[ -e "$repo" ] || { echo "ERROR: Path not found: $repo" >&2; exit 1; }
git -C "$repo" rev-parse --git-dir >/dev/null 2>&1 || { echo "ERROR: Not a Git repository: $repo" >&2; exit 1; }
echo "Path: $repo"
echo "Type: $( [ "$(git -C "$repo" rev-parse --is-bare-repository)" = true ] && echo bare || echo normal )"
echo "Branch: $(git -C "$repo" branch --show-current 2>/dev/null || echo detached)"
echo "Remotes:"
git -C "$repo" remote -v || true
echo
echo "Worktrees:"
git -C "$repo" worktree list --porcelain || true
echo
echo "FSCK:"
git -C "$repo" fsck --full || true
EOF

cat > "$WORKROOT/git.setup.worktree.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
repo="${1:-}"
worktree="${2:-}"
branch="${3:-v2}"
[ -n "$repo" ] || { echo "ERROR: repository path required" >&2; exit 1; }
[ -n "$worktree" ] || { echo "ERROR: worktree path required" >&2; exit 1; }
[ -e "$repo" ] || { echo "ERROR: repository not found: $repo" >&2; exit 1; }
git -C "$repo" rev-parse --git-dir >/dev/null 2>&1 || { echo "ERROR: not a Git repository: $repo" >&2; exit 1; }
mkdir -p "$(dirname "$worktree")"
if [ -e "$worktree" ]; then
  git -C "$repo" worktree list --porcelain | grep -Fq "worktree $worktree" && { echo "Worktree already exists: $worktree"; exit 0; }
  echo "ERROR: path already exists: $worktree" >&2
  exit 1
fi
if git -C "$repo" show-ref --verify --quiet "refs/heads/$branch"; then
  git --git-dir="$repo" worktree add "$worktree" "$branch"
else
  git --git-dir="$repo" worktree add -b "$branch" "$worktree"
fi
echo "Created worktree: $worktree"
EOF

cat > "$WORKROOT/git.view.docs.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
doc="${1:-/home/intel145/websites/dev/docs/RECOMMENDATIONS.md}"
[ -f "$doc" ] || { echo "ERROR: documentation not found: $doc" >&2; exit 1; }
command -v less >/dev/null 2>&1 && less "$doc" || cat "$doc"
EOF

cat > "$WORKROOT/docs/RECOMMENDATIONS.md" <<'EOF'
# Git Repo Recommendations

## Layout
- Use `/home/intel145/websites/dev` for worktrees.
- Use `/home/intel145/repos` for bare repositories.
- Keep each project isolated in its own folder.

## Example
- `/home/intel145/websites/dev/games/hangman/hangman.test`
- `/home/intel145/repos/games/hangman/hangman.repository`

## Rule of thumb
- Separate repo per app/game when versioning is independent.
- Use one shared repo only when projects are tightly coupled.

## Workflow
1. Create the bare repo.
2. Create the worktree.
3. Edit in the worktree.
4. Commit and push to origin.

## Notes
- The menu scripts validate paths before acting.
- Dangling objects from `git fsck` are usually harmless.
EOF

chmod +x "$WORKROOT"/*.sh
echo "Installed to: $WORKROOT"
echo "Run: $WORKROOT/git.master.menu.sh"
