#!/usr/bin/env bash
#
# test_all.sh - {{REPO_NAME}} Test Orchestrator
#
# {{PROJECT_DESCRIPTION}}
#
# This script runs all test suites across the repository in a coordinated fashion.
# Adapted from: BlackRoad Operating System Test Orchestrator Pattern
#
# Usage:
#   ./test_all.sh              # Best-effort mode (run all suites, report summary)
#   ./test_all.sh --strict     # Strict mode (fail on first error)
#   ./test_all.sh --suite <name>  # Run specific suite only
#   ./test_all.sh --help       # Show usage
#
# Available suites: {{LIST_YOUR_SUITES_HERE}}
#   Example: backend, frontend, api, sdk, docs
#

set -uo pipefail

###############################################################################
# CONFIGURATION
###############################################################################

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"

STRICT_MODE=false
SPECIFIC_SUITE=""
VERBOSE=false

# Color codes for pretty output
if [[ -t 1 ]]; then
  RED='\033[0;31m'
  GREEN='\033[0;32m'
  YELLOW='\033[1;33m'
  BLUE='\033[0;34m'
  CYAN='\033[0;36m'
  BOLD='\033[1m'
  RESET='\033[0m'
else
  RED='' GREEN='' YELLOW='' BLUE='' CYAN='' BOLD='' RESET=''
fi

# Results tracking
declare -A SUITE_RESULTS
declare -A SUITE_TIMES
SUITES_RAN=0
SUITES_PASSED=0
SUITES_FAILED=0
SUITES_SKIPPED=0

###############################################################################
# HELPERS
###############################################################################

have() {
  command -v "$1" >/dev/null 2>&1
}

log_header() {
  echo ""
  echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
  echo -e "${BOLD}$1${RESET}"
  echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
}

log_suite() {
  echo ""
  echo -e "${BLUE}▶ $1${RESET}"
}

log_info() {
  echo -e "  ${CYAN}ℹ${RESET} $1"
}

log_success() {
  echo -e "  ${GREEN}✓${RESET} $1"
}

log_warning() {
  echo -e "  ${YELLOW}⚠${RESET} $1"
}

log_error() {
  echo -e "  ${RED}✗${RESET} $1"
}

log_skip() {
  echo -e "  ${YELLOW}⊘${RESET} $1"
}

record_result() {
  local suite=$1
  local result=$2  # PASS, FAIL, SKIP
  local duration=$3

  SUITE_RESULTS[$suite]=$result
  SUITE_TIMES[$suite]=$duration
  ((SUITES_RAN++))

  case $result in
    PASS) ((SUITES_PASSED++)) ;;
    FAIL) ((SUITES_FAILED++)) ;;
    SKIP) ((SUITES_SKIPPED++)) ;;
  esac

  if [[ "$result" == "FAIL" && "$STRICT_MODE" == "true" ]]; then
    log_error "Strict mode enabled - aborting on first failure"
    print_summary
    exit 1
  fi
}

print_summary() {
  echo ""
  log_header "TEST SUMMARY"
  echo ""

  # Summary table
  printf "${BOLD}%-25s %-10s %-15s${RESET}\n" "Suite" "Result" "Duration"
  echo "─────────────────────────────────────────────────────────"

  # TODO: Update this list with your actual suites
  for suite in example-suite-1 example-suite-2 example-suite-3; do
    if [[ -n "${SUITE_RESULTS[$suite]:-}" ]]; then
      result="${SUITE_RESULTS[$suite]}"
      duration="${SUITE_TIMES[$suite]}"

      case $result in
        PASS)
          printf "${GREEN}%-25s %-10s %-15s${RESET}\n" "$suite" "✓ PASS" "$duration"
          ;;
        FAIL)
          printf "${RED}%-25s %-10s %-15s${RESET}\n" "$suite" "✗ FAIL" "$duration"
          ;;
        SKIP)
          printf "${YELLOW}%-25s %-10s %-15s${RESET}\n" "$suite" "⊘ SKIP" "$duration"
          ;;
      esac
    fi
  done

  echo "─────────────────────────────────────────────────────────"
  echo ""
  echo -e "${BOLD}Total:${RESET} $SUITES_RAN suites | ${GREEN}$SUITES_PASSED passed${RESET} | ${RED}$SUITES_FAILED failed${RESET} | ${YELLOW}$SUITES_SKIPPED skipped${RESET}"
  echo ""

  if [[ $SUITES_FAILED -gt 0 ]]; then
    echo -e "${RED}${BOLD}❌ TESTS FAILED${RESET}"
    return 1
  else
    echo -e "${GREEN}${BOLD}✅ ALL TESTS PASSED${RESET}"
    return 0
  fi
}

###############################################################################
# TEST SUITE FUNCTIONS
# TODO: Customize these for your project!
###############################################################################

# EXAMPLE SUITE 1: Replace with your actual test suite
run_example_suite_1() {
  log_suite "Example Suite 1 (Description)"

  local start_time=$(date +%s)

  # Check if suite exists
  if [[ ! -d "$ROOT/path/to/suite1" ]]; then
    log_skip "path/to/suite1 directory not found"
    record_result "example-suite-1" "SKIP" "0s"
    return 0
  fi

  cd "$ROOT/path/to/suite1"

  # TODO: Add your test commands here
  # Examples:
  # - pytest -v
  # - npm test
  # - go test ./...
  # - cargo test

  log_info "Running tests..."
  # YOUR_TEST_COMMAND_HERE

  local exit_code=$?
  local end_time=$(date +%s)
  local duration=$((end_time - start_time))

  if [[ $exit_code -eq 0 ]]; then
    log_success "Example Suite 1 tests passed"
    record_result "example-suite-1" "PASS" "${duration}s"
  else
    log_error "Example Suite 1 tests failed"
    record_result "example-suite-1" "FAIL" "${duration}s"
    return 1
  fi

  cd "$ROOT"
}

# EXAMPLE SUITE 2: Add more suites as needed
run_example_suite_2() {
  log_suite "Example Suite 2 (Description)"

  local start_time=$(date +%s)

  if [[ ! -d "$ROOT/path/to/suite2" ]]; then
    log_skip "path/to/suite2 directory not found"
    record_result "example-suite-2" "SKIP" "0s"
    return 0
  fi

  # TODO: Implement your test logic

  cd "$ROOT"
}

# Add more suite functions as needed...

###############################################################################
# COMMAND-LINE PARSING
###############################################################################

show_help() {
  cat << EOF
${BOLD}{{REPO_NAME}} - Test Orchestrator${RESET}

${BOLD}USAGE:${RESET}
  ./test_all.sh [OPTIONS]

${BOLD}OPTIONS:${RESET}
  --strict              Fail on first test suite failure (default: best-effort)
  --suite <name>        Run specific test suite only
  --verbose, -v         Show verbose test output
  --help, -h            Show this help message

${BOLD}AVAILABLE SUITES:${RESET}
  example-suite-1      Description of suite 1
  example-suite-2      Description of suite 2
  # TODO: Update this list with your actual suites

${BOLD}EXAMPLES:${RESET}
  ./test_all.sh                     # Run all suites, best-effort mode
  ./test_all.sh --strict            # Run all suites, fail-fast mode
  ./test_all.sh --suite example-suite-1     # Run only suite 1
  ./test_all.sh --suite example-suite-1 --verbose  # Verbose output

${BOLD}EXIT CODES:${RESET}
  0    All tests passed
  1    One or more test suites failed

EOF
}

while [[ $# -gt 0 ]]; do
  case $1 in
    --strict)
      STRICT_MODE=true
      shift
      ;;
    --suite)
      SPECIFIC_SUITE="$2"
      shift 2
      ;;
    --verbose|-v)
      VERBOSE=true
      shift
      ;;
    --help|-h)
      show_help
      exit 0
      ;;
    *)
      echo -e "${RED}Unknown option: $1${RESET}"
      echo "Use --help for usage information"
      exit 1
      ;;
  esac
done

###############################################################################
# MAIN EXECUTION
###############################################################################

log_header "{{REPO_NAME}} - Test Orchestrator"

if [[ "$STRICT_MODE" == "true" ]]; then
  log_info "Mode: ${RED}${BOLD}STRICT${RESET} (fail-fast)"
else
  log_info "Mode: ${GREEN}${BOLD}BEST-EFFORT${RESET} (run all suites)"
fi

if [[ -n "$SPECIFIC_SUITE" ]]; then
  log_info "Running suite: ${BOLD}$SPECIFIC_SUITE${RESET}"
fi

echo ""

# TODO: Update this section with your actual suite functions
if [[ -z "$SPECIFIC_SUITE" ]]; then
  # Run all suites
  run_example_suite_1 || true
  run_example_suite_2 || true
  # Add more suite calls here...
else
  # Run specific suite
  case $SPECIFIC_SUITE in
    example-suite-1)
      run_example_suite_1
      ;;
    example-suite-2)
      run_example_suite_2
      ;;
    # Add more cases here...
    *)
      log_error "Unknown suite: $SPECIFIC_SUITE"
      echo "Use --help to see available suites"
      exit 1
      ;;
  esac
fi

# Print summary and exit with appropriate code
print_summary
exit $?
