#!/usr/bin/env bash
# Agent build verification — run from project root after Phase 5 BUILD
set -euo pipefail

echo "=== CFM Survey SDK — Agent Build Verification ==="

STUB_MATCHES=$(grep -r "not yet implemented" src/ components/ app/ 2>/dev/null || true)
if [ -n "$STUB_MATCHES" ]; then
  echo "FAIL: Stub placeholder text found:"
  echo "$STUB_MATCHES"
  exit 1
fi
echo "OK: No stub placeholder text"

INTERNAL_IMPORTS=$(grep -rE "@explorer02/cfm-survey-sdk/src|@repo/sdk/src" src/ components/ app/ 2>/dev/null || true)
if [ -n "$INTERNAL_IMPORTS" ]; then
  echo "FAIL: Internal SDK imports found:"
  echo "$INTERNAL_IMPORTS"
  exit 1
fi
echo "OK: No internal SDK imports"

# Wizard-ready artifacts (Phase 5 — mandatory)
THEME_CSS=""
for candidate in src/styles/survey-theme.css styles/survey-theme.css app/globals.css; do
  if [ -f "$candidate" ] && grep -q "cfm-" "$candidate" 2>/dev/null; then
    THEME_CSS="$candidate"
    break
  fi
done
if [ -z "$THEME_CSS" ]; then
  for candidate in src/styles/survey-theme.css styles/survey-theme.css; do
    if [ -f "$candidate" ]; then THEME_CSS="$candidate"; break; fi
  done
fi
if [ -z "$THEME_CSS" ]; then
  echo "FAIL: src/styles/survey-theme.css missing — copy from docs/templates/survey-theme.css"
  exit 1
fi
echo "OK: survey-theme.css present ($THEME_CSS)"

SEL_STYLES=""
for candidate in src/lib/surveyUi/selectionStyles.ts lib/surveyUi/selectionStyles.ts; do
  if [ -f "$candidate" ]; then SEL_STYLES="$candidate"; break; fi
done
if [ -z "$SEL_STYLES" ]; then
  echo "FAIL: selectionStyles.ts missing — copy from docs/templates/selectionStyles.ts"
  exit 1
fi
echo "OK: selectionStyles.ts present ($SEL_STYLES)"

LBL_STYLES=""
for candidate in src/lib/surveyUi/labelStyles.ts lib/surveyUi/labelStyles.ts; do
  if [ -f "$candidate" ]; then LBL_STYLES="$candidate"; break; fi
done
if [ -z "$LBL_STYLES" ]; then
  echo "FAIL: labelStyles.ts missing — copy from docs/templates/labelStyles.ts"
  exit 1
fi
echo "OK: labelStyles.ts present ($LBL_STYLES)"

if [ ! -f "./survey-ui-config.json" ]; then
  echo "FAIL: ./survey-ui-config.json missing — draft config required for wizard seed"
  exit 1
fi
echo "OK: survey-ui-config.json present"

CHROME_HOOKS=$(grep -rE "data-cfm-(logo|btn-next|survey-title)" src/ components/ app/ 2>/dev/null || true)
if [ -z "$CHROME_HOOKS" ]; then
  echo "FAIL: No data-cfm-* chrome hooks found — copy templates/docs/templates/Header.tsx etc.; see wizard-chrome-contract.md"
  exit 1
fi
echo "OK: data-cfm-* chrome hooks present"

# Wizard layout toggle targets (each must exist for live preview toggles)
for hook in "data-cfm-logo" "data-cfm-company" "data-cfm-progress" "cfm-footer" "data-cfm-question-number" "data-cfm-required"; do
  FOUND=$(grep -rE "$hook" src/ components/ app/ 2>/dev/null || true)
  if [ -z "$FOUND" ]; then
    echo "FAIL: Missing wizard hook $hook — see wizard-chrome-contract.md"
    exit 1
  fi
done
echo "OK: wizard layout + branding hooks present"

if grep -rE '\bUI_CONFIG\b' src/ components/ app/ 2>/dev/null | grep -v uiConfig.ts | grep -v survey-ui-config; then
  echo "FAIL: Bare UI_CONFIG reference — use src/lib/uiConfig.ts (see wizard-chrome-contract.md)"
  exit 1
fi
echo "OK: No bare UI_CONFIG references"

PREVIEW_BRIDGE=""
for candidate in src/lib/previewBridge.ts lib/previewBridge.ts; do
  if [ -f "$candidate" ]; then PREVIEW_BRIDGE="$candidate"; break; fi
done
if [ -z "$PREVIEW_BRIDGE" ]; then
  echo "FAIL: src/lib/previewBridge.ts missing — copy from templates/previewBridge.ts"
  exit 1
fi
if ! grep -q "layoutFlags" "$PREVIEW_BRIDGE"; then
  echo "FAIL: previewBridge.ts outdated — re-copy from templates/previewBridge.ts (needs layoutFlags handler)"
  exit 1
fi
echo "OK: previewBridge.ts present with layoutFlags"

REQUIRED_TYPES=(MCQ TEXTFIELD NPS_SCALE CFM_MATRIX CSAT_MATRIX RATING_MATRIX SLIDER_MATRIX FILE_UPLOAD TEXT_AND_MEDIA HEATMAP RANK_ORDER)
QUESTION_FILE=""
for candidate in src/components/Question.tsx components/Question.tsx app/components/Question.tsx; do
  if [ -f "$candidate" ]; then
    QUESTION_FILE="$candidate"
    break
  fi
done

if [ -z "$QUESTION_FILE" ]; then
  echo "WARN: Question.tsx not found in expected paths — skip type coverage check"
else
  echo "Checking dispatcher types in $QUESTION_FILE"
  for t in "${REQUIRED_TYPES[@]}"; do
    if ! grep -q "QUESTION_TYPE.${t}" "$QUESTION_FILE" && \
       ! grep -q "type === '${t}'" "$QUESTION_FILE" && \
       ! grep -q "type === \"${t}\"" "$QUESTION_FILE"; then
      echo "FAIL: Missing dispatcher branch for type: ${t}"
      exit 1
    fi
  done
  if grep -q "subType" "$QUESTION_FILE"; then
    echo "FAIL: Legacy subType routing found in Question.tsx — use flat question.type literals"
    exit 1
  fi
  echo "OK: All 11 question.type branches present"

  if grep -q "RANK_ORDER" "$QUESTION_FILE" || grep -q "RankOrderScale" "$QUESTION_FILE"; then
    RANK_FILE=""
    for candidate in src/components/RankOrderScale.tsx components/RankOrderScale.tsx app/components/RankOrderScale.tsx; do
      if [ -f "$candidate" ]; then RANK_FILE="$candidate"; break; fi
    done
    if [ -z "$RANK_FILE" ]; then
      echo "FAIL: RankOrderScale.tsx required when RANK_ORDER is wired in Question.tsx"
      exit 1
    fi
    echo "OK: RankOrderScale.tsx exists ($RANK_FILE)"
    if ! grep -q "@dnd-kit/core" package.json 2>/dev/null; then
      echo "FAIL: @dnd-kit/core missing from package.json (required for RANK_ORDER)"
      exit 1
    fi
    echo "OK: @dnd-kit in package.json"
    if ! grep -q "getVisibleRankOrderOptionsForAnswers" "$QUESTION_FILE"; then
      echo "WARN: getVisibleRankOrderOptionsForAnswers not found in Question.tsx"
    fi
  fi

  if grep -q "FILE_UPLOAD" "$QUESTION_FILE" || grep -q "FileUploadScale" "$QUESTION_FILE"; then
    UPLOAD_FILE=""
    for candidate in src/components/FileUploadScale.tsx components/FileUploadScale.tsx app/components/FileUploadScale.tsx; do
      if [ -f "$candidate" ]; then UPLOAD_FILE="$candidate"; break; fi
    done
    if [ -z "$UPLOAD_FILE" ]; then
      echo "FAIL: FileUploadScale.tsx required when FILE_UPLOAD is wired"
      exit 1
    fi
    echo "OK: FileUploadScale.tsx exists ($UPLOAD_FILE)"
  fi

  require_scale_file() {
    local type_pattern="$1"
    local scale_name="$2"
    if grep -q "$type_pattern" "$QUESTION_FILE" || grep -q "$scale_name" "$QUESTION_FILE"; then
      local SCALE_FILE=""
      for candidate in "src/components/${scale_name}.tsx" "components/${scale_name}.tsx" "app/components/${scale_name}.tsx"; do
        if [ -f "$candidate" ]; then SCALE_FILE="$candidate"; break; fi
      done
      if [ -z "$SCALE_FILE" ]; then
        echo "FAIL: ${scale_name}.tsx required when ${type_pattern} is wired in Question.tsx"
        exit 1
      fi
      echo "OK: ${scale_name}.tsx exists ($SCALE_FILE)"
    fi
  }

  require_scale_file "SLIDER_MATRIX" "SliderMatrixScale"
  require_scale_file "NPS_SCALE" "RatingScale"
  require_scale_file "CFM_MATRIX" "LikertMatrixScale"
  require_scale_file "CSAT_MATRIX" "CsatMatrixScale"
  require_scale_file "RATING_MATRIX" "CsatMatrixScale"
  require_scale_file "HEATMAP" "HeatmapScale"

  if grep -qE "SLIDER_MATRIX|displayStyle.*graphics|RATING_MATRIX" "$QUESTION_FILE" 2>/dev/null; then
    TRACK_FILE=""
    for candidate in src/components/CustomSliderTrack.tsx components/CustomSliderTrack.tsx app/components/CustomSliderTrack.tsx; do
      if [ -f "$candidate" ]; then TRACK_FILE="$candidate"; break; fi
    done
    if [ -z "$TRACK_FILE" ]; then
      echo "FAIL: CustomSliderTrack.tsx required for SLIDER_MATRIX or graphics matrix"
      exit 1
    fi
    echo "OK: CustomSliderTrack.tsx exists ($TRACK_FILE)"
  fi

  if grep -q "getVisibleMcqOptionsForAnswers\|getVisibleMatrixItemsForAnswers" "$QUESTION_FILE"; then
    if ! grep -q "customFieldValues" "$QUESTION_FILE"; then
      echo "WARN: Question.tsx uses visibility helpers but may not pass customFieldValues 4th arg"
    else
      echo "OK: Question.tsx references customFieldValues for visibility helpers"
    fi
  fi
fi

SURVEY_PAGE=""
for candidate in src/components/SurveyPage.tsx components/SurveyPage.tsx app/components/SurveyPage.tsx; do
  if [ -f "$candidate" ]; then SURVEY_PAGE="$candidate"; break; fi
done

if [ -n "$SURVEY_PAGE" ]; then
  if grep -q "customFieldValues" "$SURVEY_PAGE" || grep -q "CUSTOM_FIELD_VALUES" "$SURVEY_PAGE"; then
    echo "OK: SurveyPage wires customFieldValues or CUSTOM_FIELD_VALUES"
    if grep -q "CUSTOM_FIELD_VALUES" "$SURVEY_PAGE"; then
      LIB_FILE=""
      for candidate in src/lib/customFieldValues.ts lib/customFieldValues.ts; do
        if [ -f "$candidate" ]; then LIB_FILE="$candidate"; break; fi
      done
      if [ -z "$LIB_FILE" ]; then
        echo "WARN: SurveyPage imports CUSTOM_FIELD_VALUES but src/lib/customFieldValues.ts not found"
      else
        echo "OK: customFieldValues lib file exists ($LIB_FILE)"
      fi
    fi
  else
    echo "WARN: SurveyPage found but no customFieldValues wiring — required when survey uses CUSTOM_FIELD logic"
  fi
fi

echo "Running npm run build..."
npm run build
echo "=== All checks passed ==="
