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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
OUT_DIR="$ROOT_DIR/docs/consolidation"
STAMP="$(date +%Y%m%d-%H%M%S)"
LATEST="$OUT_DIR/INTEGRATIONS_STATUS_LATEST.md"
REPORT="$OUT_DIR/INTEGRATIONS_STATUS_${STAMP}.md"

mkdir -p "$OUT_DIR"

section() {
  printf "\n## %s\n\n" "$1"
}

write_status() {
  local name="$1"; shift
  local status="$1"; shift
  local details="$*"
  printf -- "- **%s**: %s" "$name" "$status"
  if [[ -n "$details" ]]; then
    printf " - %s" "$details"
  fi
  printf "\n"
}

{
  printf "# Integrations Bootstrap Status\n\n"
  printf "Generated: %s\n\n" "$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
  printf "Repo: %s\n\n" "$ROOT_DIR"

  section "Gitea"
  gitea_code="$(curl -sS -o /dev/null -w '%{http_code}' http://127.0.0.1:3000/ || true)"
  if [[ "$gitea_code" == "200" || "$gitea_code" == "302" ]]; then
    write_status "gitea_http" "OK" "http_code=$gitea_code (http://127.0.0.1:3000)"
  else
    write_status "gitea_http" "DEGRADED" "http_code=$gitea_code (http://127.0.0.1:3000)"
  fi

  gitea_remotes="$(git -C "$ROOT_DIR" remote -v | awk '/gitea/ {print $1" "$2}' | sort -u || true)"
  if [[ -n "$gitea_remotes" ]]; then
    write_status "gitea_git_remote" "OK" "$(echo "$gitea_remotes" | tr '\n' '; ' | sed 's/; $//')"
  else
    write_status "gitea_git_remote" "INFO" "no remote URL containing 'gitea' found"
  fi

  section "GitHub"
  if command -v gh >/dev/null 2>&1; then
    gh_status="$(gh auth status 2>&1 || true)"
    if echo "$gh_status" | rg -q "Logged in to github.com"; then
      account_line="$(echo "$gh_status" | awk -F'account ' '/Logged in to github.com account/{print $2}' | head -n1)"
      write_status "gh_auth" "OK" "$account_line"
    else
      write_status "gh_auth" "DEGRADED" "gh installed but not authenticated"
    fi
  else
    write_status "gh_auth" "DEGRADED" "gh CLI not installed"
  fi

  gh_remotes="$(git -C "$ROOT_DIR" remote -v | awk '/github.com/ {print $1" "$2}' | sort -u || true)"
  if [[ -n "$gh_remotes" ]]; then
    write_status "github_git_remote" "OK" "$(echo "$gh_remotes" | tr '\n' '; ' | sed 's/; $//')"
  else
    write_status "github_git_remote" "INFO" "no remote URL containing 'github.com' found"
  fi

  section "Jupyter"
  if command -v jupyter >/dev/null 2>&1; then
    jlist="$(jupyter server list 2>/dev/null || true)"
    running="$(echo "$jlist" | sed '/^\s*$/d' | wc -l | tr -d ' ')"
    write_status "jupyter_cli" "OK" "jupyter found in PATH"
    write_status "jupyter_servers" "INFO" "listed_rows=$running"
  elif python3 -m jupyter --version >/dev/null 2>&1; then
    write_status "jupyter_cli" "OK" "python3 -m jupyter available"
    jlist="$(python3 -m jupyter server list 2>/dev/null || true)"
    running="$(echo "$jlist" | sed '/^\s*$/d' | wc -l | tr -d ' ')"
    write_status "jupyter_servers" "INFO" "listed_rows=$running"
  else
    write_status "jupyter_cli" "DEGRADED" "jupyter not found in PATH"
  fi

  nb_dir="/Users/apple/notebooks"
  if [[ -d "$nb_dir" ]]; then
    nb_count="$(find "$nb_dir" -maxdepth 1 -type f -name '*.ipynb' | wc -l | tr -d ' ')"
    write_status "notebooks_dir" "OK" "$nb_dir (ipynb_count=$nb_count)"
  else
    write_status "notebooks_dir" "DEGRADED" "$nb_dir missing"
  fi

  section "Pieces"
  pext_dir="$HOME/.cursor/extensions"
  if [[ -d "$pext_dir" ]]; then
    pieces_hits="$(find "$pext_dir" -maxdepth 1 -type d -iname '*pieces*' | wc -l | tr -d ' ')"
    if [[ "$pieces_hits" -gt 0 ]]; then
      write_status "pieces_extension" "OK" "cursor extensions matched=$pieces_hits"
    else
      write_status "pieces_extension" "DEGRADED" "no Pieces extension in $pext_dir"
    fi
  else
    write_status "pieces_extension" "DEGRADED" "$pext_dir missing"
  fi

  pieces_data_dir="$HOME/Library/Application Support/Pieces"
  if [[ -d "$pieces_data_dir" ]]; then
    write_status "pieces_data_dir" "OK" "$pieces_data_dir"
  else
    write_status "pieces_data_dir" "INFO" "$pieces_data_dir not found"
  fi

  section "Next"
  printf -- "- Run docs sync dry-run: bash scripts/docs/docs_sync.sh --dry-run\n"
  printf -- "- Apply sync to remotes: bash scripts/docs/docs_sync.sh --apply --targets github,gitea\n"
} > "$REPORT"

cp "$REPORT" "$LATEST"
echo "Wrote: $REPORT"
echo "Updated: $LATEST"
