#!/bin/bash

# Vulnerability Report Merge Script
# Function: Merge all vulnerability JSON files in the specified directory into a single JSONL report

set -e

# --- Help Information ---
usage() {
    echo "Usage: $0 <report_output_directory>"
    echo ""
    echo "Function:"
    echo "  Find all vulnerability JSON files in the specified directory and merge them into a full_report.jsonl file"
    echo ""
    echo "Parameters:"
    echo "  report_output_directory : Target directory containing individual vulnerability JSON files"
    echo ""
    echo "Output:"
    echo "  full_report.jsonl : Merged complete JSONL report file"
    echo ""
    echo "Description:"
    echo "  1. Find all files matching *-report-*.json"
    echo "  2. Automatically remove \`\`\`json wrapper markers from files"
    echo "  3. Remove all line breaks, writing each JSON object as a single line"
    echo "  4. Generate standard JSONL format (one JSON object per line)"
    echo "  5. Do not merge full_report.jsonl itself"
    exit 1
}

# --- Parameter Parsing ---

if [ $# -eq 0 ]; then
    usage
fi

REPORT_DIR="$1"
OUTPUT_FILE="${REPORT_DIR}/full_report.jsonl"

# Validate that the report directory exists
if [ ! -d "$REPORT_DIR" ]; then
    echo "Error: Report directory [$REPORT_DIR] does not exist"
    exit 1
fi

# Validate that the directory is readable
if [ ! -r "$REPORT_DIR" ]; then
    echo "Error: Report directory [$REPORT_DIR] is not readable"
    exit 1
fi

# Validate that the directory is writable (for creating full_report.jsonl)
if [ ! -w "$REPORT_DIR" ]; then
    echo "Error: Report directory [$REPORT_DIR] is not writable"
    exit 1
fi

# Clear output file
> "$OUTPUT_FILE"

echo "Merging reports..."
echo "Report directory: $REPORT_DIR"
echo "Output file: $OUTPUT_FILE"
echo ""

# Find all vulnerability JSON files (exclude full_report.jsonl itself)
# Use find to handle filenames containing spaces
mapfile -t md_files < <(find "$REPORT_DIR" -type f -name "*-report-*.json" ! -name "full_report.jsonl" 2>/dev/null | sort)

# Check if any files were found
if [ ${#md_files[@]} -eq 0 ]; then
    echo "Warning: No .json files found in directory [$REPORT_DIR]"
    exit 0
fi

echo "Found ${#md_files[@]} report files"
echo ""

# Merge all JSON files into JSONL format
for json_file in "${md_files[@]}"; do
    # Read file content
    content=$(cat "$json_file")

    # Remove possible ```json``` wrapper markers
    content=$(echo "$content" | sed '1s/^```json\s*//' | sed '$s/```\s*$//')

    # Remove all line breaks, convert to single-line JSON
    content=$(echo "$content" | tr -d '\n' | tr -d '\r')

    # Write to output file as a single line
    echo "$content" >> "$OUTPUT_FILE"
done

echo ""
echo "----------------------------------------"
echo "Report merge complete!"
echo "Merged file: $OUTPUT_FILE"
echo "Files merged: ${#md_files[@]}"
echo "----------------------------------------"

exit 0
