#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn
# generate-models-md.sh - Generate MODELS.md and/or MODELS-PERFORMANCE.md
# Part of t1012 (leaderboard), t1133 (global/per-repo split)
#
# Usage:
#   generate-models-md.sh [--mode MODE] [--output PATH] [--repo-path PATH] [--quiet]
#   generate-models-md.sh help
#
# Modes:
#   all          Combined file (default, backward-compatible)
#   global       Global data only: model catalog, routing tiers, pricing
#                Safe to propagate across repos (overwrites are harmless)
#   performance  Per-repo data only: leaderboard, task type breakdown, contest results
#                Generated from local pattern-tracker data — unique per repo
#
# Data sources:
#   1. Model registry DB (model catalog, pricing, tiers) — used by global + all
#   2. Pattern-tracker (memory.db — success/failure rates by model) — used by performance + all
#   3. Response-scoring DB (head-to-head contest results, quality scores) — used by performance + all
#
# Output:
#   --mode all:         MODELS.md (combined, legacy)
#   --mode global:      MODELS.md (global data only)
#   --mode performance: MODELS-PERFORMANCE.md (per-repo performance data)

set -euo pipefail

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit 1
source "${SCRIPT_DIR}/shared-constants.sh"
init_log_file

readonly MEMORY_DB="${AIDEVOPS_MEMORY_DIR:-$HOME/.aidevops/.agent-workspace/memory}/memory.db"
readonly SCORING_DB="${SCORING_DB_OVERRIDE:-$HOME/.aidevops/.agent-workspace/response-scoring.db}"
readonly REGISTRY_DB="${MODEL_REGISTRY_DB:-$HOME/.aidevops/.agent-workspace/model-registry.db}"
readonly PATTERN_TYPES="'SUCCESS_PATTERN','FAILURE_PATTERN','WORKING_SOLUTION','FAILED_APPROACH','ERROR_FIX'"
readonly SUCCESS_TYPES="'SUCCESS_PATTERN','WORKING_SOLUTION'"
readonly FAILURE_TYPES="'FAILURE_PATTERN','FAILED_APPROACH','ERROR_FIX'"

# Defaults
OUTPUT_PATH=""
REPO_PATH=""
QUIET=0
MODE="all"
REPO_FILTER="" # Deprecated: use --repo-path instead (kept for backward compat)

log_info() {
	[[ "$QUIET" -eq 1 ]] && return 0
	echo -e "${BLUE}[INFO]${NC} $*"
	return 0
}
log_success() {
	[[ "$QUIET" -eq 1 ]] && return 0
	echo -e "${GREEN}[OK]${NC} $*"
	return 0
}
log_warn() {
	echo -e "${YELLOW}[WARN]${NC} $*"
	return 0
}
log_error() {
	echo -e "${RED}[ERROR]${NC} $*" >&2
	return 0
}

#######################################
# Find the repo root for default output path
# Returns: repo root path or empty string
#######################################
find_repo_root() {
	git rev-parse --show-toplevel 2>/dev/null || echo ""
	return 0
}

#######################################
# Build a SQL WHERE clause fragment for repo-path filtering
# Uses the learnings.project_path column (FTS5 UNINDEXED)
# Arguments: none (reads global REPO_PATH)
# Outputs: SQL fragment to stdout (empty string if no filter)
#######################################
repo_path_filter() {
	if [[ -z "$REPO_PATH" ]]; then
		echo ""
		return 0
	fi
	# Match exact path or any subdirectory/worktree of the repo
	# project_path may contain worktree paths like /repo-feature-x/
	local escaped_path
	escaped_path=$(printf '%s' "$REPO_PATH" | sed "s/'/''/g")
	echo "AND (project_path = '${escaped_path}' OR project_path LIKE '${escaped_path}/%' OR project_path LIKE '%/$(basename "$REPO_PATH")%')"
	return 0
}

#######################################
# Build SQL AND clause for repo filtering on project_path
# Uses REPO_FILTER global. Returns empty string if no filter set.
# Arguments: none (reads REPO_FILTER global)
# Outputs: SQL clause fragment to stdout (e.g., "AND project_path LIKE '%/reponame%'")
#######################################
repo_filter_clause() {
	if [[ -n "$REPO_FILTER" ]]; then
		echo "AND project_path LIKE '%/${REPO_FILTER}%'"
	fi
	return 0
}

#######################################
# Check if a SQLite database exists and has data
# Arguments: db_path, table_name
# Returns: 0 if table has rows, 1 otherwise
#######################################
db_has_data() {
	local db_path="$1"
	local table_name="$2"

	# Validate table name is a safe identifier (prevent SQL injection)
	if [[ ! "$table_name" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
		log_error "db_has_data: invalid table name: $table_name"
		return 1
	fi

	[[ -f "$db_path" ]] || return 1
	local count
	count=$(sqlite3 "$db_path" "SELECT COUNT(*) FROM $table_name;" 2>/dev/null) || return 1
	[[ "$count" -gt 0 ]] && return 0
	return 1
}

#######################################
# Generate the model catalog section from registry DB
# Outputs: markdown table to stdout
#######################################
generate_catalog() {
	if ! [[ -f "$REGISTRY_DB" ]]; then
		echo "No model registry database found."
		echo ""
		return 0
	fi

	local count
	count=$(sqlite3 "$REGISTRY_DB" "SELECT COUNT(*) FROM models;" 2>/dev/null) || count=0
	if [[ "$count" -eq 0 ]]; then
		echo "No models registered yet."
		echo ""
		return 0
	fi

	echo "| Model | Provider | Tier | Context | Input/1M | Output/1M |"
	echo "|-------|----------|------|---------|----------|-----------|"

	sqlite3 -separator '|' "$REGISTRY_DB" "
        SELECT
            model_id,
            provider,
            CASE tier
                WHEN 'high' THEN 'opus'
                WHEN 'medium' THEN 'sonnet'
                WHEN 'low' THEN 'haiku'
                ELSE tier
            END,
            CASE
                WHEN context_window >= 1000000 THEN (context_window / 1000000) || 'M'
                ELSE (context_window / 1000) || 'K'
            END,
            printf('\$%.2f', input_price),
            printf('\$%.2f', output_price)
        FROM models
        ORDER BY
            CASE tier WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 ELSE 4 END,
            provider,
            model_id;
    " 2>/dev/null | while IFS='|' read -r model provider tier ctx input output; do
		echo "| $model | $provider | $tier | $ctx | $input | $output |"
	done

	echo ""
	return 0
}

#######################################
# Generate the routing tiers section from subagent_models
# Outputs: markdown table to stdout
#######################################
generate_routing_tiers() {
	if ! [[ -f "$REGISTRY_DB" ]]; then
		return 0
	fi

	local count
	count=$(sqlite3 "$REGISTRY_DB" "SELECT COUNT(*) FROM subagent_models;" 2>/dev/null) || count=0
	if [[ "$count" -eq 0 ]]; then
		return 0
	fi

	echo "## Routing Tiers"
	echo ""
	echo "Active model assignments for each dispatch tier:"
	echo ""
	echo "| Tier | Primary Model | Relative Cost |"
	echo "|------|---------------|---------------|"

	# Resolve canonical model names from the models table.
	# subagent_models may store versioned provider-specific names,
	# while models stores canonical names.
	# Use a correlated subquery to find the longest matching canonical name,
	# falling back to sm.model_id if no match or if already canonical.
	sqlite3 -separator '|' "$REGISTRY_DB" "
        SELECT
            sm.tier,
            COALESCE(
                (SELECT m.model_id FROM models m
                 WHERE sm.model_id LIKE m.model_id || '%'
                 ORDER BY LENGTH(m.model_id) DESC
                 LIMIT 1),
                sm.model_id
            ),
            CASE sm.tier
                WHEN 'haiku' THEN '~0.33x'
                WHEN 'flash' THEN '~0.20x'
                WHEN 'sonnet' THEN '1x (baseline)'
                WHEN 'pro' THEN '~1.5x'
                WHEN 'opus' THEN '~1.7x'
                ELSE '?'
            END
        FROM subagent_models sm
        ORDER BY
            CASE sm.tier
                WHEN 'haiku' THEN 1
                WHEN 'flash' THEN 2
                WHEN 'sonnet' THEN 3
                WHEN 'pro' THEN 4
                WHEN 'opus' THEN 5
                ELSE 6
            END;
    " 2>/dev/null | while IFS='|' read -r tier model cost; do
		echo "| $tier | $model | $cost |"
	done

	echo ""
	return 0
}

#######################################
# Generate the performance leaderboard from pattern-tracker data
# Outputs: markdown table to stdout
#######################################
generate_leaderboard() {
	if ! [[ -f "$MEMORY_DB" ]]; then
		echo "No pattern data available yet. Run tasks to build the leaderboard."
		echo ""
		return 0
	fi

	local path_filter
	path_filter=$(repo_path_filter)

	local total
	total=$(sqlite3 "$MEMORY_DB" "
        SELECT COUNT(*) FROM learnings
        WHERE type IN ($PATTERN_TYPES) $path_filter;
    " 2>/dev/null) || total=0

	if [[ "$total" -eq 0 ]]; then
		echo "No pattern data available yet. Run tasks to build the leaderboard."
		echo ""
		return 0
	fi

	echo "| Model | Tasks | Successes | Failures | Success Rate | Last Used |"
	echo "|-------|-------|-----------|----------|--------------|-----------|"

	# Query each known model tier
	local tiers="opus sonnet pro flash haiku"
	for tier in $tiers; do
		local successes failures last_used
		successes=$(sqlite3 "$MEMORY_DB" "
            SELECT COUNT(*) FROM learnings
            WHERE type IN ($SUCCESS_TYPES)
            AND (tags LIKE '%model:${tier}%' OR content LIKE '%[model:${tier}]%')
            $path_filter;
        " 2>/dev/null) || successes=0

		failures=$(sqlite3 "$MEMORY_DB" "
            SELECT COUNT(*) FROM learnings
            WHERE type IN ($FAILURE_TYPES)
            AND (tags LIKE '%model:${tier}%' OR content LIKE '%[model:${tier}]%')
            $path_filter;
        " 2>/dev/null) || failures=0

		local tasks_total=$((successes + failures))
		[[ "$tasks_total" -eq 0 ]] && continue

		local rate
		if [[ "$tasks_total" -gt 0 ]]; then
			rate=$(((successes * 100) / tasks_total))
		else
			rate=0
		fi

		last_used=$(sqlite3 "$MEMORY_DB" "
            SELECT SUBSTR(MAX(created_at), 1, 10) FROM learnings
            WHERE type IN ($PATTERN_TYPES)
            AND (tags LIKE '%model:${tier}%' OR content LIKE '%[model:${tier}]%')
            $path_filter;
        " 2>/dev/null) || last_used="—"
		[[ -z "$last_used" ]] && last_used="—"

		echo "| $tier | $tasks_total | $successes | $failures | ${rate}% | $last_used |"
	done

	echo ""
	return 0
}

#######################################
# Generate performance breakdown by task type
# Outputs: markdown table to stdout
#######################################
generate_task_type_breakdown() {
	if ! [[ -f "$MEMORY_DB" ]]; then
		return 0
	fi

	local path_filter
	path_filter=$(repo_path_filter)

	local total
	total=$(sqlite3 "$MEMORY_DB" "
        SELECT COUNT(*) FROM learnings
        WHERE type IN ($PATTERN_TYPES) $path_filter;
    " 2>/dev/null) || total=0
	[[ "$total" -eq 0 ]] && return 0

	local has_data=0
	local task_types="feature bugfix refactor code-review docs testing deployment security architecture planning research content seo"
	local rows=""

	for task_type in $task_types; do
		local successes failures
		successes=$(sqlite3 "$MEMORY_DB" "
            SELECT COUNT(*) FROM learnings
            WHERE type IN ($SUCCESS_TYPES)
            AND (tags LIKE '%${task_type}%' OR content LIKE '%[task:${task_type}]%')
            $path_filter;
        " 2>/dev/null) || successes=0

		failures=$(sqlite3 "$MEMORY_DB" "
            SELECT COUNT(*) FROM learnings
            WHERE type IN ($FAILURE_TYPES)
            AND (tags LIKE '%${task_type}%' OR content LIKE '%[task:${task_type}]%')
            $path_filter;
        " 2>/dev/null) || failures=0

		local task_total=$((successes + failures))
		[[ "$task_total" -eq 0 ]] && continue

		has_data=1
		local rate=$(((successes * 100) / task_total))
		rows+="| $task_type | $task_total | $successes | $failures | ${rate}% |"$'\n'
	done

	if [[ "$has_data" -eq 1 ]]; then
		echo "### By Task Type"
		echo ""
		echo "| Task Type | Tasks | Successes | Failures | Success Rate |"
		echo "|-----------|-------|-----------|----------|--------------|"
		printf '%s' "$rows"
		echo ""
	fi

	return 0
}

#######################################
# Generate contest results from response-scoring DB
# Outputs: markdown section to stdout
#######################################
generate_contest_results() {
	if ! [[ -f "$SCORING_DB" ]]; then
		echo "No contest data available yet. Run \`/compare-models\` or enable contest mode (t1011) to generate data."
		echo ""
		return 0
	fi

	local response_count
	response_count=$(sqlite3 "$SCORING_DB" "SELECT COUNT(*) FROM responses;" 2>/dev/null) || response_count=0
	if [[ "$response_count" -eq 0 ]]; then
		echo "No contest data available yet."
		echo ""
		return 0
	fi

	# Model quality scores (from scored responses)
	local score_count
	score_count=$(sqlite3 "$SCORING_DB" "SELECT COUNT(*) FROM scores;" 2>/dev/null) || score_count=0

	if [[ "$score_count" -gt 0 ]]; then
		echo "### Quality Scores"
		echo ""
		echo "Weighted average across all evaluated responses (correctness 30%, completeness 25%, code quality 25%, clarity 20%):"
		echo ""
		echo "| Model | Responses | Avg Score | Avg Time (s) |"
		echo "|-------|-----------|-----------|--------------|"

		sqlite3 -separator '|' "$SCORING_DB" "
            SELECT
                r.model_id,
                COUNT(DISTINCT r.response_id),
                printf('%.2f', AVG(ws.weighted_score)),
                CASE WHEN AVG(r.response_time) = 0.0 THEN NULL
                     ELSE printf('%.1f', AVG(r.response_time))
                END
            FROM responses r
            JOIN (
                SELECT response_id,
                       SUM(CASE criterion
                           WHEN 'correctness'  THEN score * 0.30
                           WHEN 'completeness' THEN score * 0.25
                           WHEN 'code_quality' THEN score * 0.25
                           WHEN 'clarity'      THEN score * 0.20
                           ELSE 0 END) / NULLIF(
                           SUM(CASE criterion
                               WHEN 'correctness'  THEN 0.30
                               WHEN 'completeness' THEN 0.25
                               WHEN 'code_quality' THEN 0.25
                               WHEN 'clarity'      THEN 0.20
                               ELSE 0 END), 0) AS weighted_score
                FROM scores
                GROUP BY response_id
            ) ws ON r.response_id = ws.response_id
            GROUP BY r.model_id
            ORDER BY AVG(ws.weighted_score) DESC;
		" 2>/dev/null | while IFS='|' read -r model responses avg_score avg_time; do
			echo "| $model | $responses | $avg_score/5.0 | ${avg_time:-N/A} |"
		done

		echo ""
	fi

	# Head-to-head comparison wins
	local comparison_count
	comparison_count=$(sqlite3 "$SCORING_DB" "SELECT COUNT(*) FROM comparisons;" 2>/dev/null) || comparison_count=0

	if [[ "$comparison_count" -gt 0 ]]; then
		echo "### Head-to-Head Results"
		echo ""
		echo "| Model | Wins | Contests |"
		echo "|-------|------|----------|"

		sqlite3 -separator '|' "$SCORING_DB" "
            SELECT
                r.model_id,
                COUNT(*),
                (SELECT COUNT(*) FROM comparisons c2
                 JOIN responses r2 ON c2.prompt_id = r2.prompt_id
                 WHERE r2.model_id = r.model_id) as total_contests
            FROM comparisons c
            JOIN responses r ON c.winner_id = r.response_id
            GROUP BY r.model_id
            ORDER BY COUNT(*) DESC;
        " 2>/dev/null | while IFS='|' read -r model wins contests; do
			echo "| $model | $wins | $contests |"
		done

		echo ""
	fi

	return 0
}

#######################################
# Generate the overall stats summary
# Outputs: markdown to stdout
#######################################
generate_stats_summary() {
	local path_filter
	path_filter=$(repo_path_filter)
	local pattern_total=0
	local scoring_total=0

	if [[ -f "$MEMORY_DB" ]]; then
		pattern_total=$(sqlite3 "$MEMORY_DB" "
            SELECT COUNT(*) FROM learnings WHERE type IN ($PATTERN_TYPES) $path_filter;
        " 2>/dev/null) || pattern_total=0
	fi

	if [[ -f "$SCORING_DB" ]]; then
		scoring_total=$(sqlite3 "$SCORING_DB" "SELECT COUNT(*) FROM responses;" 2>/dev/null) || scoring_total=0
	fi

	echo "- **Pattern data points**: $pattern_total"
	echo "- **Scored responses**: $scoring_total"
	if [[ -n "$REPO_PATH" ]]; then
		echo "- **Scope**: $(basename "$REPO_PATH") (repo-specific)"
	else
		echo "- **Scope**: Global (all repos)"
	fi

	if [[ -f "$MEMORY_DB" ]] && [[ "$pattern_total" -gt 0 ]]; then
		local oldest newest
		oldest=$(sqlite3 "$MEMORY_DB" "
            SELECT SUBSTR(MIN(created_at), 1, 10) FROM learnings WHERE type IN ($PATTERN_TYPES) $path_filter;
        " 2>/dev/null) || oldest="—"
		newest=$(sqlite3 "$MEMORY_DB" "
            SELECT SUBSTR(MAX(created_at), 1, 10) FROM learnings WHERE type IN ($PATTERN_TYPES) $path_filter;
        " 2>/dev/null) || newest="—"
		echo "- **Date range**: $oldest to $newest"
	fi

	echo ""
	return 0
}

#######################################
# Generate global-only MODELS.md (safe to propagate across repos)
# Contains: model catalog, routing tiers, pricing — no per-repo data
# Arguments: output path
#######################################
generate_global_md() {
	local output="$1"
	local timestamp
	timestamp=$(date -u '+%Y-%m-%dT%H:%M:%SZ')

	{
		echo "# Available Models"
		echo ""
		echo "Global model catalog, routing tiers, and pricing."
		echo "Auto-generated by \`generate-models-md.sh --mode global\` — do not edit manually."
		echo "Per-repo performance data is in \`MODELS-PERFORMANCE.md\`."
		echo ""
		echo "**Last updated**: $timestamp"
		echo ""
		echo "## Model Catalog"
		echo ""
		generate_catalog
		generate_routing_tiers
		echo "---"
		echo ""
		echo "*Generated by [aidevops](https://github.com/marcusquinn/aidevops) t1012, t1133*"
	} >"$output"

	return 0
}

#######################################
# Generate per-repo MODELS-PERFORMANCE.md (unique per repo)
# Contains: leaderboard, task type breakdown, contest results
# Arguments: output path
#######################################
generate_performance_md() {
	local output="$1"
	local timestamp
	timestamp=$(date -u '+%Y-%m-%dT%H:%M:%SZ')

	{
		echo "# Model Performance"
		echo ""
		echo "Per-repo performance data from pattern-tracker and response-scoring databases."
		echo "Auto-generated by \`generate-models-md.sh --mode performance\` — do not edit manually."
		echo "Global model catalog is in \`MODELS.md\`."
		if [[ -n "$REPO_PATH" ]]; then
			echo "Filtered by repo: \`$(basename "$REPO_PATH")\`"
		fi
		echo ""
		echo "**Last updated**: $timestamp"
		echo ""
		generate_stats_summary
		echo "## Performance Leaderboard"
		echo ""
		echo "Success rates from autonomous task execution (pattern-tracker data):"
		echo ""
		generate_leaderboard
		generate_task_type_breakdown
		echo "## Contest Results"
		echo ""
		echo "Quality evaluations from model comparison sessions (response-scoring data):"
		echo ""
		generate_contest_results
		echo "---"
		echo ""
		echo "*Generated by [aidevops](https://github.com/marcusquinn/aidevops) t1012, t1133*"
	} >"$output"

	return 0
}

#######################################
# Main: assemble the full MODELS.md (legacy combined mode)
#######################################
generate_models_md() {
	local output="$1"
	local timestamp
	timestamp=$(date -u '+%Y-%m-%dT%H:%M:%SZ')

	{
		echo "# Model Leaderboard"
		echo ""
		if [[ -n "$REPO_PATH" ]]; then
			echo "Per-repo performance data for **$(basename "$REPO_PATH")** from pattern-tracker and response-scoring databases."
		else
			echo "Live performance data from pattern-tracker and response-scoring databases."
		fi
		echo "Auto-generated by \`generate-models-md.sh\` — do not edit manually."
		echo ""
		echo "**Last updated**: $timestamp"
		echo ""
		generate_stats_summary
		echo "## Available Models"
		echo ""
		generate_catalog
		generate_routing_tiers
		echo "## Performance Leaderboard"
		echo ""
		echo "Success rates from autonomous task execution (pattern-tracker data):"
		echo ""
		generate_leaderboard
		generate_task_type_breakdown
		echo "## Contest Results"
		echo ""
		echo "Quality evaluations from model comparison sessions (response-scoring data):"
		echo ""
		generate_contest_results
		echo "---"
		echo ""
		echo "*Generated by [aidevops](https://github.com/marcusquinn/aidevops) t1012, t1133*"
	} >"$output"

	return 0
}

#######################################
# Show help
#######################################
cmd_help() {
	echo "generate-models-md.sh - Generate MODELS.md and/or MODELS-PERFORMANCE.md"
	echo ""
	echo "Usage:"
	echo "  generate-models-md.sh [--mode MODE] [--output PATH] [--repo-path PATH] [--quiet]"
	echo "  generate-models-md.sh help"
	echo ""
	echo "Modes:"
	echo "  all          Combined file (default, backward-compatible)"
	echo "  global       Global data only: catalog, tiers, pricing → MODELS.md"
	echo "  performance  Per-repo data only: leaderboard, contests → MODELS-PERFORMANCE.md"
	echo ""
	echo "Options:"
	echo "  --mode MODE         Generation mode: all, global, performance (default: all)"
	echo "  --output PATH       Output file path (default depends on mode)"
	echo "  --repo-path PATH    Filter pattern data to this repo path (per-repo tracking)"
	echo "  --repo-filter NAME  Filter performance data by repo name (matches project_path)"
	echo "  --quiet             Suppress info messages"
	echo ""
	echo "When --repo-path is provided, pattern data is filtered to only include"
	echo "learnings recorded against that project path, giving per-repo performance"
	echo "insights. Without it, global stats across all repos are shown."
	echo ""
	echo "Data sources:"
	echo "  Model registry:    $REGISTRY_DB"
	echo "  Pattern tracker:   $MEMORY_DB"
	echo "  Response scoring:  $SCORING_DB"
	return 0
}

# Parse arguments
while [[ $# -gt 0 ]]; do
	case "$1" in
	--mode)
		if [[ $# -lt 2 ]]; then
			log_error "--mode requires a value"
			exit 1
		fi
		MODE="$2"
		if [[ "$MODE" != "all" && "$MODE" != "global" && "$MODE" != "performance" ]]; then
			log_error "Invalid mode: $MODE (must be: all, global, performance)"
			exit 1
		fi
		shift 2
		;;
	--repo-filter)
		if [[ $# -lt 2 ]]; then
			log_error "--repo-filter requires a value"
			exit 1
		fi
		REPO_FILTER="$2"
		# Sanitize for safe SQL interpolation (escape single quotes)
		REPO_FILTER="${REPO_FILTER//\'/\'\'}"
		shift 2
		;;
	--output)
		if [[ $# -lt 2 ]]; then
			log_error "--output requires a value"
			exit 1
		fi
		OUTPUT_PATH="$2"
		shift 2
		;;
	--repo-path)
		if [[ $# -lt 2 ]]; then
			log_error "--repo-path requires a value"
			exit 1
		fi
		REPO_PATH="$2"
		shift 2
		;;
	--quiet)
		QUIET=1
		shift
		;;
	help | --help | -h)
		cmd_help
		exit 0
		;;
	*)
		log_error "Unknown argument: $1"
		cmd_help
		exit 1
		;;
	esac
done

# Bridge --repo-filter to --repo-path for backward compatibility
# repo_path_filter() (used by all generation functions) reads REPO_PATH, not REPO_FILTER
if [[ -n "$REPO_FILTER" && -z "$REPO_PATH" ]]; then
	REPO_PATH="$REPO_FILTER"
fi

# Determine output path based on mode
if [[ -z "$OUTPUT_PATH" ]]; then
	local_repo_root="$(find_repo_root)"
	base_dir="${local_repo_root:-.}"
	case "$MODE" in
	global) OUTPUT_PATH="${base_dir}/MODELS.md" ;;
	performance) OUTPUT_PATH="${base_dir}/MODELS-PERFORMANCE.md" ;;
	all) OUTPUT_PATH="${base_dir}/MODELS.md" ;;
	esac
fi

# Verify sqlite3 is available
if ! command -v sqlite3 &>/dev/null; then
	log_error "sqlite3 is required but not found"
	exit 1
fi

log_info "Generating ($MODE mode) from live data..."
log_info "  Registry: $REGISTRY_DB"
log_info "  Patterns: $MEMORY_DB"
log_info "  Scoring:  $SCORING_DB"
if [[ -n "$REPO_PATH" ]]; then
	log_info "  Repo filter: $REPO_PATH"
fi

case "$MODE" in
global)
	generate_global_md "$OUTPUT_PATH"
	;;
performance)
	generate_performance_md "$OUTPUT_PATH"
	;;
all)
	generate_models_md "$OUTPUT_PATH"
	;;
esac

log_success "Generated $OUTPUT_PATH"
