#!/usr/bin/env bash
# Tests for scrumMaster PDCA state machine implementation
# Tests actual scrumMaster commands and PDCA cycle behavior

#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x

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

source this
source test.suite
source state

log.level $level

# Test machine name for isolation
TEST_PDCA="PDCA_SCRUMTEST_$$"

# Cleanup function
cleanup_pdca() {
  rm -f "$CONFIG_PATH/stateMachines/${TEST_PDCA}.states.env" 2>/dev/null
  rm -f "$CONFIG_PATH/stateMachines/${TEST_PDCA}.pdca.env" 2>/dev/null
  rm -f "$CONFIG_PATH/stateMachines/PDCA.states.env" 2>/dev/null
  rm -f "$CONFIG_PATH/stateMachines/PDCA.pdca.env" 2>/dev/null
}

# Cleanup before tests
cleanup_pdca

# ============================================================================
# T1: Test pdca.start creates machine
# ============================================================================
test.case - "T1: pdca.start creates PDCA machine" \
  ./scrumMaster pdca.start PDCA 0

if state machine.exists PDCA 2>/dev/null; then
  create.result 0 "PLANNING"
else
  create.result 1 "Machine not created"
fi
expect 0 "PLANNING" "scrumMaster pdca.start creates machine"

# ============================================================================
# T2: Test pdca.state shows current state
# ============================================================================
test.case - "T2: pdca.state shows PLANNING" \
  ./scrumMaster pdca.state PDCA

# Current state should be planning after start
CURRENT=$(./scrumMaster pdca.state PDCA 2>/dev/null | tail -1)
if [ "$CURRENT" = "PLANNING" ]; then
  create.result 0 "PLANNING"
else
  create.result 1 "Got $CURRENT instead of PLANNING"
fi
expect 0 "PLANNING" "pdca.state returns correct state"

# ============================================================================
# T3: Test pdca.next advances state
# ============================================================================
test.case - "T3: pdca.next advances from PLANNING to DOING" \
  ./scrumMaster pdca.next PDCA

# Note: test.case already called pdca.next once, so we're now at DOING
CURRENT=$(./scrumMaster pdca.state PDCA 2>/dev/null | grep -E "^[A-Z_]+$" | tail -1)
if [ "$CURRENT" = "DOING" ]; then
  create.result 0 "DOING"
else
  create.result 1 "Got $CURRENT instead of DOING"
fi
expect 0 "DOING" "pdca.next advances state correctly"

# ============================================================================
# T4: Test pdca.reset clears machine
# ============================================================================
test.case - "T4: pdca.reset clears machine" \
  ./scrumMaster pdca.reset PDCA

./scrumMaster pdca.reset PDCA >/dev/null 2>&1
if ! state machine.exists PDCA 2>/dev/null; then
  create.result 0 "RESET"
else
  create.result 1 "Machine still exists"
fi
expect 0 "RESET" "pdca.reset removes machine"

# ============================================================================
# T5: Test pdca.run happy path (0 errors) -> FINISHED
# ============================================================================
cleanup_pdca
test.case - "T5: pdca.run with 0 errors -> FINISHED" \
  ./scrumMaster pdca.run PDCA 0

# Check the final state after run (test.case already ran it)
FINAL=$(./scrumMaster pdca.state PDCA 2>/dev/null | grep -E "^[A-Z_]+$" | tail -1)
if [ "$FINAL" = "FINISHED" ]; then
  create.result 0 "FINISHED"
else
  create.result 1 "Got $FINAL instead of FINISHED"
fi
expect 0 "FINISHED" "pdca.run with 0 errors completes successfully"

# ============================================================================
# T6: Test pdca.run with 2 errors (C->A loops)
# ============================================================================
cleanup_pdca
test.case - "T6: pdca.run with 2 errors -> FINISHED after C->A loops" \
  ./scrumMaster pdca.run PDCA 2

FINAL=$(./scrumMaster pdca.state PDCA 2>/dev/null | grep -E "^[A-Z_]+$" | tail -1)
if [ "$FINAL" = "FINISHED" ]; then
  create.result 0 "FINISHED"
else
  create.result 1 "Got $FINAL instead of FINISHED"
fi
expect 0 "FINISHED" "pdca.run with errors loops C->A and finishes"

# ============================================================================
# T7: Test max iterations guard (too many errors)
# ============================================================================
cleanup_pdca
test.case - "T7: pdca.run with 10 errors (max 5 iterations) -> ERROR_MAX_ITERATIONS" \
  ./scrumMaster pdca.run PDCA 10

FINAL=$(./scrumMaster pdca.state PDCA 2>/dev/null | grep -E "^[A-Z_]+$" | tail -1)
if [ "$FINAL" = "ERROR_MAX_ITERATIONS" ]; then
  create.result 0 "ERROR_MAX_ITERATIONS"
else
  create.result 1 "Got $FINAL instead of ERROR_MAX_ITERATIONS"
fi
expect 0 "ERROR_MAX_ITERATIONS" "pdca.run stops at max iterations"

# ============================================================================
# T8: Test pdca.errors shows/sets error count
# ============================================================================
cleanup_pdca
./scrumMaster pdca.start PDCA 5 >/dev/null 2>&1

test.case - "T8: pdca.errors shows error count" \
  ./scrumMaster pdca.errors PDCA

# Source the config to check the variable
source $CONFIG_PATH/stateMachines/PDCA.pdca.env 2>/dev/null
if [ "$PDCA_ERROR_COUNT" = "5" ]; then
  create.result 0 "5"
else
  create.result 1 "Got $PDCA_ERROR_COUNT instead of 5"
fi
expect 0 "5" "pdca.errors correctly tracks error count"

# ============================================================================
# T9: Test iteration counter increments during C->A loops
# ============================================================================
cleanup_pdca
./scrumMaster pdca.run PDCA 3 >/dev/null 2>&1

test.case - "T9: Iteration counter tracks C->A loops" \
  source $CONFIG_PATH/stateMachines/PDCA.pdca.env 2>/dev/null

source $CONFIG_PATH/stateMachines/PDCA.pdca.env 2>/dev/null
# With 3 errors, should have 3 iterations (fixing one per iteration)
if [ "$PDCA_ITERATION" -ge 3 ]; then
  create.result 0 "$PDCA_ITERATION iterations"
else
  create.result 1 "Only $PDCA_ITERATION iterations, expected >= 3"
fi
expect 0 "$PDCA_ITERATION iterations" "Iteration counter tracks correctly"

# ============================================================================
# Cleanup
# ============================================================================
cleanup_pdca

test.suite.save.results

