#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/lib/json-node.sh"
source "$SCRIPT_DIR/lib/codex-config.sh"
require_node

case "$(uname -s)" in
    MINGW*|MSYS*|CYGWIN*) IS_WINDOWS=true ;;
    *) IS_WINDOWS=false ;;
esac

MODEL_PROFILE="mixed"
while [ $# -gt 0 ]; do
  case "$1" in
    --model-profile)
      shift
      if [ $# -eq 0 ]; then
        echo "Missing value for --model-profile (expected: mixed or maximum)" >&2
        exit 1
      fi
      MODEL_PROFILE="$1"
      ;;
    --model-profile=*)
      MODEL_PROFILE="${1#*=}"
      ;;
  esac
  shift
done

case "$MODEL_PROFILE" in
  mixed|maximum) ;;
  *)
    echo "Unsupported model profile: $MODEL_PROFILE (expected: mixed or maximum)" >&2
    exit 1
    ;;
esac

WIZARD_VERSION="$(json_get_file "$SCRIPT_DIR/package.json" 'data.version || "unknown"')"
[ -z "$WIZARD_VERSION" ] && WIZARD_VERSION="unknown"

print_feedback_guidance() {
  local command_name="$1"
  local failure_point="$2"
  local repo_shape="${3:-unknown}"

  echo ""
  echo "Likely wizard-level failure detected."
  echo "Report bugs or improvements back to codex-rdlc-wizard."
  echo "No issue will be posted automatically."
  echo "Issue-ready details:"
  echo "  wizard version: $WIZARD_VERSION"
  echo "  command: $command_name"
  echo "  repo shape: $repo_shape"
  echo "  failure point: $failure_point"
}

require_bundle_file() {
  local path="$1"
  local label="$2"
  if [ ! -f "$path" ]; then
    print_feedback_guidance "install" "missing bundled runtime file: $label" "unknown"
    exit 1
  fi
}

for required in \
  "AGENTS.md" \
  "RDLC.md" \
  "RDLC-LOOP.md" \
  "START-RDLC.md" \
  "PROVE-IT.md" \
  "start-rdlc.sh" \
  "start-rdlc.ps1" \
  ".codex/config.toml" \
  ".codex/hooks.json" \
  ".codex/unix-hooks.json" \
  ".codex/windows-hooks.json" \
  ".codex/hooks/bash-guard.sh" \
  ".codex/hooks/session-start.sh" \
  ".codex/hooks/rdlc-prompt-check.sh" \
  ".codex/hooks/git-guard.cjs" \
  ".codex/hooks/session-start.cjs" \
  ".codex/hooks/git-guard.ps1" \
  ".codex/hooks/session-start.ps1" \
  ".agents/skills/rdlc/SKILL.md"; do
  require_bundle_file "$SCRIPT_DIR/$required" "$required"
done

CODEX_HOME_DIR="${CODEX_HOME:-$HOME/.codex}"
SKILLS_ROOT="$CODEX_HOME_DIR/skills"
SKILLS_BACKUP_ROOT="$CODEX_HOME_DIR/backups/skills"
GLOBAL_HELPER_SKILLS=("feedback" "setup-wizard" "update-wizard")

copy_if_missing() {
  local source="$1"
  local target="$2"
  local label="$3"
  if [ ! -f "$target" ]; then
    cp "$source" "$target"
    echo "Created $label"
  else
    echo "$label already exists - skipping (review manually)"
  fi
}

install_repo_skill() {
  local name="$1"
  local source="$SCRIPT_DIR/.agents/skills/$name/SKILL.md"
  local target=".agents/skills/$name/SKILL.md"

  mkdir -p "$(dirname "$target")"
  if [ -f "$target" ]; then
    echo "$target already exists - skipping (review manually)"
  else
    cp "$source" "$target"
    echo "Installed $target"
  fi
}

install_global_skill() {
  local skill_path="$1"
  local skill_name

  [ -d "$skill_path" ] || return 0

  skill_name="$(basename "$skill_path")"
  if [ -d "$SKILLS_ROOT/$skill_name" ]; then
    cp -R "$SKILLS_ROOT/$skill_name" "$SKILLS_BACKUP_ROOT/$skill_name.bak.$(date +%s)"
    rm -rf "$SKILLS_ROOT/$skill_name"
    echo "Backed up existing Codex skill: $skill_name"
  fi
  cp -R "$skill_path" "$SKILLS_ROOT/"
  echo "Installed Codex skill: $skill_name"
}

global_skill_matches_bundle() {
  local skill_name="$1"
  local bundled_path="$SCRIPT_DIR/skills/$skill_name"
  local target_path="$SKILLS_ROOT/$skill_name"

  [ -d "$bundled_path" ] || return 1
  [ -d "$target_path" ] || return 1

  diff -qr "$bundled_path" "$target_path" >/dev/null 2>&1
}

remove_wizard_managed_global_skill() {
  local skill_name="$1"
  local reason="$2"
  local target_path="$SKILLS_ROOT/$skill_name"

  [ -d "$target_path" ] || return 0

  if ! global_skill_matches_bundle "$skill_name"; then
    echo "Preserved user-owned global Codex skill: $skill_name"
    return 0
  fi

  mkdir -p "$SKILLS_BACKUP_ROOT"
  cp -R "$target_path" "$SKILLS_BACKUP_ROOT/$skill_name.bak.$(date +%s)"
  rm -rf "$target_path"
  echo "Removed wizard-managed global Codex skill: $skill_name ($reason)"
}

prune_legacy_global_skill() {
  local legacy_name="$1"
  local canonical_name="$2"
  local legacy_path="$SKILLS_ROOT/$legacy_name"

  [ -d "$legacy_path" ] || return 0

  mkdir -p "$SKILLS_BACKUP_ROOT"
  cp -R "$legacy_path" "$SKILLS_BACKUP_ROOT/$legacy_name.bak.$(date +%s)"
  rm -rf "$legacy_path"
  echo "Removed legacy Codex skill: $legacy_name (canonical: $canonical_name)"
}

write_model_profile() {
  mkdir -p .codex-rdlc
  cat > .codex-rdlc/model-profile.json <<EOF
{
  "selected_profile": "$MODEL_PROFILE",
  "profiles": {
    "mixed": {
      "main_model": "gpt-5.4-mini",
      "main_reasoning": "xhigh",
      "review_model": "gpt-5.5",
      "review_reasoning": "xhigh",
      "tradeoff": "Smaller/faster main model for routine work while keeping xhigh main reasoning and xhigh review."
    },
    "maximum": {
      "main_model": "gpt-5.5",
      "main_reasoning": "xhigh",
      "review_model": "gpt-5.5",
      "review_reasoning": "xhigh",
      "tradeoff": "Higher latency and token usage in exchange for maximum stability and depth."
    }
  },
  "policy": {
    "high_confidence_threshold_percent": 95,
    "low_confidence_rule": "Research more first. If confidence stays below 95%, escalate review to xhigh. Use the maximum profile for abstract, complex, or high-blast-radius work."
  }
}
EOF
  echo "Wrote .codex-rdlc/model-profile.json ($MODEL_PROFILE)"
}

echo "Installing RDLC Wizard for Codex CLI..."

copy_if_missing "$SCRIPT_DIR/AGENTS.md" "AGENTS.md" "AGENTS.md"
copy_if_missing "$SCRIPT_DIR/RDLC.md" "RDLC.md" "RDLC.md"
copy_if_missing "$SCRIPT_DIR/RDLC-LOOP.md" "RDLC-LOOP.md" "RDLC-LOOP.md"
copy_if_missing "$SCRIPT_DIR/START-RDLC.md" "START-RDLC.md" "START-RDLC.md"
copy_if_missing "$SCRIPT_DIR/PROVE-IT.md" "PROVE-IT.md" "PROVE-IT.md"

mkdir -p scripts .rdlc
copy_if_missing "$SCRIPT_DIR/templates/regression_test.sh.template" "scripts/regression_test.sh" "scripts/regression_test.sh"
copy_if_missing "$SCRIPT_DIR/templates/slop_scan.sh.template" "scripts/slop_scan.sh" "scripts/slop_scan.sh"
copy_if_missing "$SCRIPT_DIR/templates/generate_deliverable.py.template" "scripts/generate_deliverable.py" "scripts/generate_deliverable.py"
copy_if_missing "$SCRIPT_DIR/templates/slop-allowlist.txt.template" ".rdlc/slop-allowlist.txt" ".rdlc/slop-allowlist.txt"
chmod +x scripts/regression_test.sh scripts/slop_scan.sh 2>/dev/null || true
printf '%s\n' "$WIZARD_VERSION" > .rdlc/version

mkdir -p "$SKILLS_ROOT" "$SKILLS_BACKUP_ROOT"
for skill_name in "${GLOBAL_HELPER_SKILLS[@]}"; do
  install_global_skill "$SCRIPT_DIR/skills/$skill_name"
done
remove_wizard_managed_global_skill "rdlc" "repo-scoped .agents/skills/rdlc is the canonical \$rdlc entrypoint"
prune_legacy_global_skill "codex-rdlc" "rdlc"

mkdir -p .codex/hooks

merge_codex_config_profile ".codex/config.toml" "$MODEL_PROFILE"
echo "Merged repo-local Codex config for model profile '$MODEL_PROFILE'"

if [ -f ".codex/hooks.json" ]; then
  cp .codex/hooks.json ".codex/hooks.json.bak.$(date +%s)"
  echo "Backed up existing hooks.json"
fi

cp "$SCRIPT_DIR/.codex/hooks/"*.sh .codex/hooks/
chmod +x .codex/hooks/*.sh
cp "$SCRIPT_DIR/.codex/hooks/"*.cjs .codex/hooks/
rm -f .codex/hooks/git-guard.js .codex/hooks/session-start.js

if [ "$IS_WINDOWS" = "true" ]; then
  cp "$SCRIPT_DIR/.codex/windows-hooks.json" .codex/hooks.json
  cp "$SCRIPT_DIR/.codex/hooks/git-guard.ps1" .codex/hooks/
  cp "$SCRIPT_DIR/.codex/hooks/session-start.ps1" .codex/hooks/
  copy_if_missing "$SCRIPT_DIR/start-rdlc.ps1" "start-rdlc.ps1" "start-rdlc.ps1"
  echo "Installed .codex/hooks.json (universal Node hooks)"
  echo "Installed Node and PowerShell hook scripts"
else
  cp "$SCRIPT_DIR/.codex/unix-hooks.json" .codex/hooks.json
  copy_if_missing "$SCRIPT_DIR/start-rdlc.sh" "start-rdlc.sh" "start-rdlc.sh"
  chmod +x start-rdlc.sh
  echo "Installed .codex/hooks.json (universal Node hooks)"
fi

echo "Installed shell hook scripts"

install_repo_skill rdlc
write_model_profile

echo ""
echo "RDLC Wizard for Codex installed."
START_MODEL="$(profile_model "$MODEL_PROFILE")"
START_REASONING="$(profile_reasoning "$MODEL_PROFILE")"
echo "Recommended start: codex -m $START_MODEL -c 'model_reasoning_effort=\"$START_REASONING\"'"
echo "Use plain 'codex' instead if you want to rely on trusted repo-local config."
echo "Fresh-session note: if you ran this from inside an existing Codex session, exit and reopen Codex in this repo so repo-local config, hooks, and skills load."
echo "Hook review note: if Codex says hooks need review, open /hooks after restart and review pending repo hooks before relying on enforcement."
echo "Start new with selected profile: codex -m $START_MODEL -c 'model_reasoning_effort=\"$START_REASONING\"'"
echo "Resume with selected profile: codex resume -m $START_MODEL -c 'model_reasoning_effort=\"$START_REASONING\"'"
echo "If resume warns it came back with a different model, resume explicitly with: codex resume -m gpt-5.5 -c 'model_reasoning_effort=\"xhigh\"'"
echo "If you normally use yolo-style sessions, use the canonical full-trust Codex flag:"
echo "  codex --dangerously-bypass-approvals-and-sandbox -m $START_MODEL -c 'model_reasoning_effort=\"$START_REASONING\"'"
echo "  codex resume --dangerously-bypass-approvals-and-sandbox -m $START_MODEL -c 'model_reasoning_effort=\"$START_REASONING\"'"
echo "Codex may accept --yolo as shorthand; this wizard prints the canonical full-trust flag."
echo "Full-auto is not full-trust: full-trust bypasses sandbox and approval prompts."
echo "Full-trust warning: only use that variant in repos you fully trust."
echo "Model profile: '$MODEL_PROFILE'."
echo "  - mixed: gpt-5.4-mini main pass + gpt-5.5 xhigh review for better speed, lower latency, and lower token usage."
echo "  - maximum: gpt-5.5 xhigh throughout for maximum stability and the most thorough \"ultimate mode\"."
echo "Wrote repo-local .codex/config.toml model keys for this profile; mixed is wizard policy, not a native Codex mode."
echo "Codex loads project config only after the repo is trusted, and trusted project config overrides your user-level ~/.codex/config.toml."
echo "If confidence drops below 95%, research more first. If it still stays below 95%, escalate review to xhigh."
echo "Repo-scoped skills are still a work in progress. Today the supported public workflow skill is '\$rdlc'."
echo "Codex treats same-name skills from different scopes as distinct choices; normal setup installs global helper skills only and keeps repo-scoped '\$rdlc' canonical."
echo "Additional repo-scoped workflows stay unnamed until their public contracts are ready."
echo "Auth-heavy note: for Windows / WAM / MFA or other live sign-in flows, the prompt itself stays user-owned."
echo "This wizard still owns command shape, checks, and the verify/resume steps after you complete sign-in."
echo "If auth, license, tenant, or permission state decides what work is possible, add a repo-local doctor / check-capability / Test-*Access helper."
