#!/usr/bin/env bash
# ============================================================
# Finlet — pre-commit hook
# ============================================================
#
# Runs the dashboard lint guards in series. Blocks the commit if any
# guard fails:
#   1. trusted-types: bare HTML-sink writes outside trustedHTML()
#      (see scripts/lint-trusted-types.sh).
#   2. event-bus: direct addEventListener('finlet:...') /
#      dispatchEvent(new CustomEvent('finlet:...')) bypassing
#      window.finletBus (see scripts/lint-event-bus.sh).
#   3. no-legacy-edits: any staged modification under legacy/
#      (see scripts/lint-no-legacy-edits.sh).
#
# This file is checked into `scripts/git-hooks/` so it stays in
# version control. To activate it locally, run:
#
#     bash scripts/setup-git-hooks.sh
#
# The setup script symlinks this file into `.git/hooks/pre-commit`.
# ============================================================

set -uo pipefail

# Resolve the repo root from the hook's location. When invoked by git,
# `pwd` is already the repo root, but resolve from the symlink target so
# manual invocations from any cwd work too.
HOOK_PATH="${BASH_SOURCE[0]}"
# Follow the symlink (if any) to the canonical scripts/git-hooks/ copy.
if [[ -L "$HOOK_PATH" ]]; then
    HOOK_PATH="$(readlink "$HOOK_PATH")"
fi
HOOK_DIR="$(cd "$(dirname "$HOOK_PATH")" 2>/dev/null && pwd)"

# --------------------------------------------------------------
# 1. trusted-types lint
# --------------------------------------------------------------
# Try the canonical lint script location first (sibling of git-hooks/).
LINT_SCRIPT="$HOOK_DIR/../lint-trusted-types.sh"

# Fallback: locate the lint script via git rev-parse, in case this hook
# was invoked from a copied (non-symlinked) location that lost the
# canonical sibling.
if [[ ! -f "$LINT_SCRIPT" ]]; then
    REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
    if [[ -n "$REPO_ROOT" ]] && [[ -f "$REPO_ROOT/scripts/lint-trusted-types.sh" ]]; then
        LINT_SCRIPT="$REPO_ROOT/scripts/lint-trusted-types.sh"
    fi
fi

if [[ ! -f "$LINT_SCRIPT" ]]; then
    echo "pre-commit: cannot locate scripts/lint-trusted-types.sh — skipping trusted-types lint" >&2
else
    bash "$LINT_SCRIPT" --check
    status_tt=$?
    if (( status_tt != 0 )); then
        echo "" >&2
        echo "pre-commit: trusted-types lint failed. Run \`bash scripts/lint-trusted-types.sh\` for the full message." >&2
        exit "$status_tt"
    fi
fi

# --------------------------------------------------------------
# 2. event-bus lint
# --------------------------------------------------------------
LINT_BUS="$HOOK_DIR/../lint-event-bus.sh"

if [[ ! -f "$LINT_BUS" ]]; then
    REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
    if [[ -n "$REPO_ROOT" ]] && [[ -f "$REPO_ROOT/scripts/lint-event-bus.sh" ]]; then
        LINT_BUS="$REPO_ROOT/scripts/lint-event-bus.sh"
    fi
fi

if [[ ! -f "$LINT_BUS" ]]; then
    echo "pre-commit: cannot locate scripts/lint-event-bus.sh — skipping event-bus lint" >&2
else
    bash "$LINT_BUS" --check
    status_eb=$?
    if (( status_eb != 0 )); then
        echo "" >&2
        echo "pre-commit: event-bus lint failed. Run \`bash scripts/lint-event-bus.sh\` for the full message." >&2
        exit "$status_eb"
    fi
fi

# --------------------------------------------------------------
# 3. no-legacy-edits lint
# --------------------------------------------------------------
LINT_LEGACY="$HOOK_DIR/../lint-no-legacy-edits.sh"

if [[ ! -f "$LINT_LEGACY" ]]; then
    REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
    if [[ -n "$REPO_ROOT" ]] && [[ -f "$REPO_ROOT/scripts/lint-no-legacy-edits.sh" ]]; then
        LINT_LEGACY="$REPO_ROOT/scripts/lint-no-legacy-edits.sh"
    fi
fi

if [[ ! -f "$LINT_LEGACY" ]]; then
    echo "pre-commit: cannot locate scripts/lint-no-legacy-edits.sh — skipping no-legacy-edits lint" >&2
else
    bash "$LINT_LEGACY" --check
    status_nl=$?
    if (( status_nl != 0 )); then
        echo "" >&2
        echo "pre-commit: no-legacy-edits lint failed. Run \`bash scripts/lint-no-legacy-edits.sh\` for the full message." >&2
        exit "$status_nl"
    fi
fi

exit 0
