#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
WORKSPACE_DIR="${1:-}"
ARCHIVE_SCRIPT="$ROOT_DIR/scripts/archive_prod_validation_workspace.sh"

if [[ -z "$WORKSPACE_DIR" ]]; then
  echo "Usage: $0 /path/to/prod-validation-workspace" >&2
  exit 1
fi

if [[ ! -d "$WORKSPACE_DIR" ]]; then
  echo "Workspace not found: $WORKSPACE_DIR" >&2
  exit 1
fi

required_files=(
  "01-war-room-board.md"
  "02-evidence-log.md"
  "03-final-go-no-go.md"
  "04-session-outcome.md"
  "README.md"
)

missing=0
for file in "${required_files[@]}"; do
  if [[ ! -f "$WORKSPACE_DIR/$file" ]]; then
    echo "Missing required file: $WORKSPACE_DIR/$file" >&2
    missing=1
  fi
done

if [[ "$missing" -ne 0 ]]; then
  exit 1
fi

printf 'Workspace check: OK\n'
printf 'Workspace: %s\n' "$WORKSPACE_DIR"
printf '\nKey files:\n'
for file in "${required_files[@]}"; do
  printf -- '- %s\n' "$WORKSPACE_DIR/$file"
done
printf '\nArchiving...\n'
"$ARCHIVE_SCRIPT" "$WORKSPACE_DIR"
printf '\nNext actions:\n'
printf '1. Review %s\n' "$WORKSPACE_DIR/03-final-go-no-go.md"
printf '2. Review %s\n' "$WORKSPACE_DIR/04-session-outcome.md"
printf '3. If GO, follow %s\n' "$ROOT_DIR/docs/qa/release-candidate-runbook-2026-03-12.md"
