#!/usr/bin/env bash

level=$1
if [ -z "$level" ]; then
  level=1
else
  shift
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$level>"

source this
source test.suite

log.level $level

source $OOSH_DIR/myId
source $OOSH_DIR/ossh

# ============================================================================
# Phase 1: Pre-Implementation — Parameter Validation & Mandatory Checklist
# ============================================================================

# --- P2a: Missing all 3 params → error ---
test.case.expect.error 1 "P2a: no args returns error" \
  myId.create.github.deploy.key
if [ "$RETURN_VALUE" -ne 0 ]; then
  expect.pass "P2a: returns non-zero on missing all params"
else
  expect.fail "P2a: should fail with no args"
fi

# --- P2b: Missing 2 params → error ---
test.case.expect.error 1 "P2b: 1 arg only returns error" \
  myId.create.github.deploy.key github-web4x
if [ "$RETURN_VALUE" -ne 0 ]; then
  expect.pass "P2b: returns non-zero on missing githubUrl and idName"
else
  expect.fail "P2b: should fail with only sshConfigName"
fi

# --- P2c: Missing 1 param → error ---
test.case.expect.error 1 "P2c: 2 args only returns error" \
  myId.create.github.deploy.key github-web4x "git@github.com:web4x/Web4AI.git"
if [ "$RETURN_VALUE" -ne 0 ]; then
  expect.pass "P2c: returns non-zero on missing idName"
else
  expect.fail "P2c: should fail with only sshConfigName and githubUrl"
fi

# --- MC3: Completion stub exists ---
test.case $level "MC3: myId.parameter.completion.id exists" echo "checking"
if declare -f myId.parameter.completion.id >/dev/null 2>&1; then
  expect.pass "MC3: completion stub exists"
else
  expect.fail "MC3: myId.parameter.completion.id missing"
fi

# --- MC4: Human-readable error message ---
test.case $level "MC4: missing sshConfigName gives readable error" echo "checking"
ERROR_OUTPUT=$(myId.create.github.deploy.key 2>&1)
if echo "$ERROR_OUTPUT" | grep -qi "sshConfigName\|no.*provided\|usage"; then
  expect.pass "MC4: error message is human-readable"
else
  expect.fail "MC4: error should mention param name, got: $ERROR_OUTPUT"
fi

# ============================================================================
# Phase 2: Post-Implementation — Functional Tests T1-T5 + Regression R1-R4
# ============================================================================

ID_DIR="$HOME/.ssh/ids/upDown.deployKey.github-web4x"

if [ -d "$ID_DIR" ]; then

  # --- T1: myId list shows new identity ---
  test.case $level "T1: myId list shows upDown.deployKey.github-web4x" \
    myId.list
  LIST_OUTPUT=$(myId.list 2>/dev/null)
  if echo "$LIST_OUTPUT" | grep -q "upDown.deployKey.github-web4x"; then
    expect.pass "T1: new identity visible in myId list"
  else
    expect.fail "T1: upDown.deployKey.github-web4x not in myId list"
  fi

  # --- T2a: Identity directory exists ---
  test.case $level "T2a: identity directory exists" echo "checking"
  if [ -d "$ID_DIR" ]; then
    expect.pass "T2a: directory exists"
  else
    expect.fail "T2a: $ID_DIR does not exist"
  fi

  # --- T2b: id_ed25519 private key exists ---
  test.case $level "T2b: id_ed25519 key file exists" echo "checking"
  if [ -f "$ID_DIR/id_ed25519" ]; then
    expect.pass "T2b: private key exists"
  else
    expect.fail "T2b: id_ed25519 missing"
  fi

  # --- T2c: id_ed25519.pub exists ---
  test.case $level "T2c: id_ed25519.pub exists" echo "checking"
  if [ -f "$ID_DIR/id_ed25519.pub" ]; then
    expect.pass "T2c: public key exists"
  else
    expect.fail "T2c: id_ed25519.pub missing"
  fi

  # --- T2d: private_key/ subfolder copy ---
  test.case $level "T2d: private_key subfolder has copy" echo "checking"
  if [ -f "$ID_DIR/private_key/upDown.deployKey.github-web4x.private_key" ]; then
    expect.pass "T2d: private_key copy exists"
  else
    expect.fail "T2d: private_key copy missing"
  fi

  # --- T2e: public_keys/ subfolder copy ---
  test.case $level "T2e: public_keys subfolder has copy" echo "checking"
  if [ -f "$ID_DIR/public_keys/upDown.deployKey.github-web4x.public_key" ]; then
    expect.pass "T2e: public_keys copy exists"
  else
    expect.fail "T2e: public_keys copy missing"
  fi

  # --- T3: Key is ed25519 ---
  test.case $level "T3: key type is ed25519" echo "checking"
  if [ -f "$ID_DIR/id_ed25519.pub" ]; then
    KEY_HEADER=$(cat "$ID_DIR/id_ed25519.pub" | head -c 30)
    if echo "$KEY_HEADER" | grep -q "ssh-ed25519"; then
      expect.pass "T3: key is ed25519"
    else
      expect.fail "T3: key header is not ssh-ed25519, got: $KEY_HEADER"
    fi
  else
    expect.fail "T3: id_ed25519.pub missing, cannot check key type"
  fi

  # --- T4a: SSH config has Host entry ---
  test.case $level "T4a: SSH config has Host github-web4x.upDown.deployKey" echo "checking"
  if grep -q "^Host github-web4x.upDown.deployKey$" "$HOME/.ssh/config" 2>/dev/null; then
    expect.pass "T4a: Host entry exists"
  else
    expect.fail "T4a: Host github-web4x.upDown.deployKey not in ~/.ssh/config"
  fi

  # --- T4b: IdentityFile references new key ---
  test.case $level "T4b: IdentityFile points to new key" echo "checking"
  CONFIG_BLOCK=$(awk '/^Host github-web4x\.upDown\.deployKey$/,/^$/' "$HOME/.ssh/config")
  if echo "$CONFIG_BLOCK" | grep -q "upDown.deployKey.github-web4x/id_ed25519"; then
    expect.pass "T4b: IdentityFile references correct identity"
  else
    expect.fail "T4b: IdentityFile does not reference upDown.deployKey.github-web4x"
  fi

  # --- T5: ossh get.public.id prints public key ---
  test.case $level "T5: ossh get.public.id prints public key" echo "checking"
  PUB_OUTPUT=$(ossh.get.public.id upDown.deployKey.github-web4x 2>/dev/null)
  if echo "$PUB_OUTPUT" | grep -q "ssh-ed25519"; then
    expect.pass "T5: public key output contains ssh-ed25519"
  else
    expect.fail "T5: get.public.id output missing ssh-ed25519"
  fi

  # ========================================================================
  # Regression: existing cerulean.githubCC unaffected
  # ========================================================================

  # --- R1: cerulean.githubCC directory still exists ---
  test.case $level "R1: cerulean.githubCC directory still exists" echo "checking"
  if [ -d "$HOME/.ssh/ids/cerulean.githubCC" ]; then
    expect.pass "R1: cerulean.githubCC intact"
  else
    expect.fail "R1: cerulean.githubCC directory MISSING"
  fi

  # --- R2: Original RSA key intact ---
  test.case $level "R2: cerulean.githubCC has original RSA key" echo "checking"
  if [ -f "$HOME/.ssh/ids/cerulean.githubCC/id_rsa" ]; then
    expect.pass "R2: RSA key intact"
  else
    expect.fail "R2: id_rsa missing from cerulean.githubCC"
  fi

  # --- R3: Host github.com still in config ---
  test.case $level "R3: SSH config still has Host github.com" echo "checking"
  if grep -q "^Host github.com$" "$HOME/.ssh/config"; then
    expect.pass "R3: Host github.com still present"
  else
    expect.fail "R3: Host github.com MISSING from SSH config"
  fi

  # --- R4: Host github.com still references cerulean ---
  test.case $level "R4: Host github.com still points to cerulean" echo "checking"
  GH_BLOCK=$(awk '/^Host github\.com$/,/^$/' "$HOME/.ssh/config")
  if echo "$GH_BLOCK" | grep -q "cerulean.githubCC"; then
    expect.pass "R4: github.com still references cerulean.githubCC"
  else
    expect.fail "R4: github.com IdentityFile changed"
  fi

else
  echo -e "\e[1;33m  SKIPPED Phase 2: identity not yet created (waiting for expert)\e[0m"
fi

test.suite.save.results
