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

TARGET_DIR="$(pwd)"
FORCE=0
QUALITY_TEMPLATE=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --force)
      FORCE=1
      shift
      ;;
    --quality-template)
      QUALITY_TEMPLATE="${2:-}"
      shift 2
      ;;
    *)
      TARGET_DIR="$1"
      shift
      ;;
  esac
done

if [[ ! -d "$TARGET_DIR" ]]; then
  echo "[mosaic] Target directory does not exist: $TARGET_DIR" >&2
  exit 1
fi

MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
TEMPLATE_ROOT="$MOSAIC_HOME/templates/repo"

if [[ ! -d "$TEMPLATE_ROOT" ]]; then
  echo "[mosaic] Missing templates at $TEMPLATE_ROOT" >&2
  echo "[mosaic] Install or refresh framework: ~/.config/mosaic/install.sh" >&2
  exit 1
fi

mkdir -p "$TARGET_DIR/.mosaic" "$TARGET_DIR/scripts/agent"
mkdir -p "$TARGET_DIR/.mosaic/orchestrator" "$TARGET_DIR/.mosaic/orchestrator/logs" "$TARGET_DIR/.mosaic/orchestrator/results"

copy_file() {
  local src="$1"
  local dst="$2"

  if [[ -f "$dst" && "$FORCE" -ne 1 ]]; then
    echo "[mosaic] Skip existing: $dst"
    return
  fi

  cp "$src" "$dst"
  echo "[mosaic] Wrote: $dst"
}

copy_file "$TEMPLATE_ROOT/.mosaic/README.md" "$TARGET_DIR/.mosaic/README.md"
copy_file "$TEMPLATE_ROOT/.mosaic/repo-hooks.sh" "$TARGET_DIR/.mosaic/repo-hooks.sh"
copy_file "$TEMPLATE_ROOT/.mosaic/quality-rails.yml" "$TARGET_DIR/.mosaic/quality-rails.yml"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/config.json" "$TARGET_DIR/.mosaic/orchestrator/config.json"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/tasks.json" "$TARGET_DIR/.mosaic/orchestrator/tasks.json"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/state.json" "$TARGET_DIR/.mosaic/orchestrator/state.json"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/matrix_state.json" "$TARGET_DIR/.mosaic/orchestrator/matrix_state.json"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/logs/.gitkeep" "$TARGET_DIR/.mosaic/orchestrator/logs/.gitkeep"
copy_file "$TEMPLATE_ROOT/.mosaic/orchestrator/results/.gitkeep" "$TARGET_DIR/.mosaic/orchestrator/results/.gitkeep"

for file in "$TEMPLATE_ROOT"/scripts/agent/*.sh; do
  base="$(basename "$file")"
  copy_file "$file" "$TARGET_DIR/scripts/agent/$base"
  chmod +x "$TARGET_DIR/scripts/agent/$base"
done

if [[ ! -f "$TARGET_DIR/AGENTS.md" ]]; then
  cat > "$TARGET_DIR/AGENTS.md" <<'AGENTS_EOF'
# Agent Guidelines

## Required Load Order

1. `~/.config/mosaic/SOUL.md`
2. `~/.config/mosaic/STANDARDS.md`
3. `~/.config/mosaic/AGENTS.md`
4. `~/.config/mosaic/guides/E2E-DELIVERY.md`
5. `AGENTS.md` (this file)
6. Runtime-specific guide: `~/.config/mosaic/runtime/<runtime>/RUNTIME.md`
7. `.mosaic/repo-hooks.sh`

## Session Lifecycle

```bash
bash scripts/agent/session-start.sh
bash scripts/agent/critical.sh
bash scripts/agent/session-end.sh
```

## Shared Tools

- Quality and orchestration guides: `~/.config/mosaic/guides/`
- Shared automation tools: `~/.config/mosaic/tools/`

## Repo-Specific Notes

- Add project constraints and workflows here.
- Implement hook functions in `.mosaic/repo-hooks.sh`.
- Scratchpads are mandatory for non-trivial tasks.
AGENTS_EOF
  echo "[mosaic] Wrote: $TARGET_DIR/AGENTS.md"
else
  echo "[mosaic] AGENTS.md exists; add standards load order if missing"
fi

echo "[mosaic] Repo bootstrap complete: $TARGET_DIR"
echo "[mosaic] Next: edit $TARGET_DIR/.mosaic/repo-hooks.sh with project workflows"
echo "[mosaic] Optional: apply quality tools via ~/.config/mosaic/bin/mosaic-quality-apply --template <template> --target $TARGET_DIR"
echo "[mosaic] Optional: run orchestrator rail via ~/.config/mosaic/bin/mosaic-orchestrator-drain"
echo "[mosaic] Optional: run detached orchestrator via bash $TARGET_DIR/scripts/agent/orchestrator-daemon.sh start"

if [[ -n "$QUALITY_TEMPLATE" ]]; then
  if [[ -x "$MOSAIC_HOME/tools/_scripts/mosaic-quality-apply" ]]; then
    "$MOSAIC_HOME/tools/_scripts/mosaic-quality-apply" --template "$QUALITY_TEMPLATE" --target "$TARGET_DIR"
    if [[ -f "$TARGET_DIR/.mosaic/quality-rails.yml" ]]; then
      sed -i "s/^enabled:.*/enabled: true/" "$TARGET_DIR/.mosaic/quality-rails.yml"
      sed -i "s/^template:.*/template: \"$QUALITY_TEMPLATE\"/" "$TARGET_DIR/.mosaic/quality-rails.yml"
    fi
    echo "[mosaic] Applied quality tools template: $QUALITY_TEMPLATE"
  else
    echo "[mosaic] WARN: mosaic-quality-apply not found; skipping quality tools apply" >&2
  fi
fi
