#!/bin/bash
# Generate porting tracker reports
# Writes reports/current.md (always) and reports/report_{commit_id}.md (snapshot)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

DB_PATH="$REPO_ROOT/porting.db"
TRACKER_PROJECT="$REPO_ROOT/tools/NatsNet.PortTracker"
CURRENT_REPORT="$SCRIPT_DIR/current.md"

# Check if DB exists
if [ ! -f "$DB_PATH" ]; then
    echo "Warning: $DB_PATH not found, skipping report generation"
    exit 0
fi

# Generate current.md
dotnet run --project "$TRACKER_PROJECT" -- report export \
    --format md \
    --output "$CURRENT_REPORT" \
    --db "$DB_PATH" 2>/dev/null || {
    echo "Warning: report generation failed, skipping"
    exit 0
}

# Generate commit-specific snapshot
COMMIT_ID=$(git -C "$REPO_ROOT" rev-parse --short HEAD 2>/dev/null || echo "unknown")
COMMIT_REPORT="$SCRIPT_DIR/report_${COMMIT_ID}.md"
cp "$CURRENT_REPORT" "$COMMIT_REPORT"

echo "Reports generated: current.md, report_${COMMIT_ID}.md"
