#!/usr/bin/env bash
# AY Framework -- HANDOFF schema validation gate.
# Validates a single HANDOFF entry against the canonical schema before it is
# appended to .ay/tracking/HANDOFFS.md. Used by /task-done and /go (Phase 10).
#
# Usage:
#   ayf-validate-handoff.sh <file>     # validate the HANDOFF block in <file>
#   ayf-validate-handoff.sh -          # read the HANDOFF block from stdin
#   printf '%s' "$entry" | ayf-validate-handoff.sh
#
# Exit: 0 = valid, 1 = invalid (per-field errors on stderr), 2 = usage error.
#
# Canonical schema (required fields, labeled, one per line):
#   To:      <agent-name | all>
#   From:    <agent-name>
#   Task:    <task-number | ->
#   Date:    <YYYY-MM-DD HH:MM>
#   Summary: <one-line summary>
#   Detail:  <free-form body>            # optional
#
# Portable: pure bash + grep/sed, no external deps. Works on Mac, Linux,
# Windows (Git Bash), matching the other hooks in this repo.

set -euo pipefail

REQUIRED_FIELDS="To From Task Date Summary"

usage() {
  echo "usage: ayf-validate-handoff.sh <file|->" >&2
  echo "       (or pipe a HANDOFF entry on stdin)" >&2
  exit 2
}

# --- Read the entry -------------------------------------------------------
if [ "$#" -gt 1 ]; then
  usage
fi

if [ "$#" -eq 1 ] && [ "$1" != "-" ]; then
  if [ ! -f "$1" ]; then
    echo "ayf-validate-handoff: file not found: $1" >&2
    exit 1
  fi
  ENTRY="$(cat "$1")"
else
  # stdin: explicit "-" or no arg
  ENTRY="$(cat)"
fi

if [ -z "${ENTRY//[[:space:]]/}" ]; then
  echo "ayf-validate-handoff: empty HANDOFF entry" >&2
  exit 1
fi

# --- Validate each required field ----------------------------------------
# A field is valid when a line "Field:" exists AND has a non-empty value.
ERRORS=""

field_value() {
  # Print the trimmed value of the first "Field:" line, or nothing.
  local field="$1"
  printf '%s\n' "$ENTRY" \
    | grep -m1 -E "^[[:space:]]*${field}:" \
    | sed -E "s/^[[:space:]]*${field}:[[:space:]]*//" \
    | sed -E 's/[[:space:]]+$//'
}

has_field() {
  local field="$1"
  printf '%s\n' "$ENTRY" | grep -qE "^[[:space:]]*${field}:"
}

for field in $REQUIRED_FIELDS; do
  if ! has_field "$field"; then
    ERRORS="${ERRORS}  - missing required field: ${field}:\n"
    continue
  fi
  value="$(field_value "$field")"
  if [ -z "$value" ]; then
    ERRORS="${ERRORS}  - field '${field}:' is present but has no value\n"
  fi
done

# --- Validate Date shape (YYYY-MM-DD HH:MM) when present with a value -----
if has_field "Date"; then
  date_value="$(field_value "Date")"
  if [ -n "$date_value" ]; then
    if ! printf '%s' "$date_value" \
        | grep -qE '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$'; then
      ERRORS="${ERRORS}  - field 'Date:' must match YYYY-MM-DD HH:MM (got: ${date_value})\n"
    fi
  fi
fi

# --- Report ---------------------------------------------------------------
if [ -n "$ERRORS" ]; then
  echo "ayf-validate-handoff: HANDOFF entry rejected -- schema violations:" >&2
  printf '%b' "$ERRORS" >&2
  echo "  Required schema: To, From, Task, Date (YYYY-MM-DD HH:MM), Summary." >&2
  exit 1
fi

exit 0
