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

# Runs on every container start (postStartCommand). Pure information —
# either SETUP REQUIRED (with next steps) or READY (with environment debug
# info). Important for users who open Claude Code directly and never touch
# a terminal where welcome.sh would otherwise print the gate.
#
# gcloud activation lives in post-create.sh (one-time) and inside `revos
# init` (when a new key is provisioned). The persisted ~/.config/gcloud
# named volume keeps the activation state across rebuilds, so this script
# doesn't need to re-activate on every start.

# --- Keep RevOS AI skills current ---------------------------------------
# Install/refresh the skills under .claude/skills/ from their source of truth
# (github.com/revosai/skills) on each container start. `skills add` is
# idempotent and self-healing: it installs the bundle if it's missing (e.g.
# the install during `revos init` failed because there was no network) and
# refreshes it to the latest otherwise. Skills are plain files, so Claude Code
# picks up any change the next time it loads them — no plugin reload needed.
# Best-effort: a network hiccup must never block startup.
project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
if command -v npx >/dev/null 2>&1; then
  (
    cd "$project_root" &&
      npx -y skills add revosai/skills/data-engineering \
        -a claude-code --copy -y >/dev/null 2>&1
  ) || true
fi

need_setup=false

if command -v revos >/dev/null 2>&1; then
  if ! revos auth status --json 2>/dev/null | jq -e '.authenticated' >/dev/null 2>&1; then
    need_setup=true
  fi
  if [ ! -s "$HOME/.revos/<%=projectSlug%>-gsa-creds.json" ]; then
    need_setup=true
  fi
fi

if $need_setup; then
  echo ""
  echo "████████████████████████████████████████████████████████████████████████"
  echo "█                                                                      █"
  echo "█   ⚠  REVOS DEV CONTAINER — SETUP REQUIRED                            █"
  echo "█                                                                      █"
  echo "█   Open a terminal and run:                                           █"
  echo "█                                                                      █"
  echo "█       revos init                                                     █"
  echo "█                                                                      █"
  echo "████████████████████████████████████████████████████████████████████████"
  echo ""
else
  # Setup is complete — print a confirmation banner with environment debug
  # info. Useful for users who never open a terminal, and as a quick
  # reference when troubleshooting "is my container looking at the right
  # project / dataset / org?".
  user_email=$(revos auth status --json 2>/dev/null | jq -r '.email // "(unknown)"')
  gcloud_account=$(gcloud config get-value account 2>/dev/null || true)
  gcloud_account=${gcloud_account:-(none)}
  gcloud_project=$(gcloud config get-value project 2>/dev/null || true)
  gcloud_project=${gcloud_project:-${GOOGLE_CLOUD_PROJECT:-(none)}}

  echo ""
  echo "████████████████████████████████████████████████████████████████████████"
  echo "█                                                                      █"
  echo "█   ✓  REVOS DEV CONTAINER — READY                                     █"
  echo "█                                                                      █"
  printf "█   RevOS user:    %-51s █\n" "${user_email:0:51}"
  printf "█   GCP account:   %-51s █\n" "${gcloud_account:0:51}"
  printf "█   GCP project:   %-51s █\n" "${gcloud_project:0:51}"
  printf "█   BQ dataset:    %-51s █\n" "${REVOS_BQ_DATASET:-(unset)}"
  printf "█   Org ID:        %-51s █\n" "${REVOS_ORG_ID:-(unset)}"
  echo "█                                                                      █"
  echo "████████████████████████████████████████████████████████████████████████"
  echo ""
fi
