#!/bin/bash
# deploy-gate.test.sh — Tests for the deploy quality gate logic
#
# Tests the evaluation gate step from deploy-to-vps.sh in isolation
# by extracting the scoring logic into a testable function.
#
# Usage: bash cli-made/scripts/tests/deploy-gate.test.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"

# Source config for thresholds
source "$PROJECT_ROOT/scripts/config.sh"

# Counters
PASSED=0
FAILED=0
TOTAL=0

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

pass() {
  ((PASSED++))
  ((TOTAL++))
  echo -e "  ${GREEN}PASS${NC} $1"
}

fail() {
  ((FAILED++))
  ((TOTAL++))
  echo -e "  ${RED}FAIL${NC} $1"
  if [[ -n "${2:-}" ]]; then
    echo -e "       ${RED}$2${NC}"
  fi
}

# ============================================================================
# Extract the deploy gate logic as a testable function
# This mirrors the logic in deploy-to-vps.sh Step 3
# ============================================================================

deploy_gate() {
  local eval_score="$1"
  local eval_grade="$2"
  local output=""

  if [[ -z "$eval_score" ]]; then
    output="Could not parse evaluation score, continuing..."
    echo "$output"
    return 0
  fi

  output="Score: ${eval_score}/100 (${eval_grade})"
  echo "$output"

  if [[ "$eval_score" -lt "$WS_EVAL_PASS" ]]; then
    echo "Deployment blocked: score ${eval_score} < ${WS_EVAL_PASS} minimum"
    return 1
  elif [[ "$eval_score" -lt "$WS_EVAL_GOOD" ]]; then
    echo "Score below GOOD threshold (${WS_EVAL_GOOD})"
    return 0
  fi

  echo "Quality evaluation passed"
  return 0
}

echo ""
echo "============================================"
echo "  Deploy Quality Gate — Test Suite"
echo "============================================"
echo ""
echo "Running tests..."
echo ""

# ============================================================================
# Test 1: Gate passes for score >= 60
# ============================================================================

GATE_OUTPUT=$(deploy_gate 85 "GOOD" 2>&1)
GATE_EXIT=$?

if [[ "$GATE_EXIT" -eq 0 ]]; then
  pass "1. Gate passes for score >= 60 (score=85)"
else
  fail "1. Gate passes for score >= 60" "Exit code was $GATE_EXIT"
fi

# ============================================================================
# Test 2: Gate shows warning for 60-79
# ============================================================================

WARN_OUTPUT=$(deploy_gate 67 "NEEDS WORK" 2>&1)
WARN_EXIT=$?

if [[ "$WARN_EXIT" -eq 0 ]] && echo "$WARN_OUTPUT" | grep -q "below GOOD threshold"; then
  pass "2. Gate shows warning for score 60-79 (score=67)"
else
  fail "2. Gate shows warning for score 60-79" "exit=$WARN_EXIT, output: $WARN_OUTPUT"
fi

# ============================================================================
# Test 3: Gate blocks for score < 60
# ============================================================================

set +e
BLOCK_OUTPUT=$(deploy_gate 45 "FAIL" 2>&1)
BLOCK_EXIT=$?
set -e

if [[ "$BLOCK_EXIT" -eq 1 ]]; then
  pass "3. Gate blocks for score < 60 (score=45, exit=1)"
else
  fail "3. Gate blocks for score < 60" "Expected exit 1, got $BLOCK_EXIT"
fi

# ============================================================================
# Test 4: Handles missing evaluate-site.sh gracefully
# ============================================================================

# Simulate the deploy script behavior when evaluate-site.sh doesn't exist
EVAL_SCRIPT="/nonexistent/evaluate-site.sh"
if [[ -f "$EVAL_SCRIPT" ]]; then
  GATE4_OUTPUT="unexpected: script exists"
else
  GATE4_OUTPUT="evaluate-site.sh not found, skipping quality check"
fi

if echo "$GATE4_OUTPUT" | grep -q "not found, skipping"; then
  pass "4. Handles missing script gracefully (prints 'not found, skipping')"
else
  fail "4. Handles missing script gracefully" "Output: $GATE4_OUTPUT"
fi

# ============================================================================
# Summary
# ============================================================================

echo ""
echo "============================================"
echo -e "  Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC} / $TOTAL total"
echo "============================================"
echo ""

if [[ "$FAILED" -gt 0 ]]; then
  exit 1
fi
exit 0
