#!/bin/bash
# 
# Common Match Comparison Workflow Example
# 
# This script demonstrates the complete workflow for comparing two models
# using only the GT objects that both models successfully matched.
#
# Usage:
#   bash eval_tools/compare_models_with_common_matches.sh

set -e  # Exit on error

# Set PYTHONPATH to project root for module imports
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
export PYTHONPATH="${PROJECT_ROOT}:${PYTHONPATH}"

echo "================================================================================"
echo "Common Match Comparison Workflow"
echo "================================================================================"

# Configuration
MODEL1_CONFIG="eval_tools/configs/eval_config_yolov5s_cncap_768-roi1.yaml"
MODEL2_CONFIG="eval_tools/configs/eval_config_yolov5s_cncap-roi1.yaml"
OUTPUT_BASE="evaluation_results/eval_results_common_match_comparison_cncap_yolov5s_768_roi1-conf0.4-v2"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

MODEL1_NAME="20260317"
MODEL2_NAME="20260228"

# Heading tolerance mode: strict, relaxed, or both
HEADING_TOLERANCE="both"

# Step 1: Evaluate Model 1 with detailed match saving
echo ""
echo "Step 1: Evaluating Model 1 (${MODEL1_NAME}) with detailed match saving..."
echo "        Heading tolerance: ${HEADING_TOLERANCE}"
echo "--------------------------------------------------------------------------------"
MODEL1_OUTPUT="${OUTPUT_BASE}/${MODEL1_NAME}/${TIMESTAMP}"
python eval_tools/core/eval.py \
  --config ${MODEL1_CONFIG} \
  --output-dir ${MODEL1_OUTPUT} \
  --heading-tolerance ${HEADING_TOLERANCE} \
  --save-detailed-matches

# Use the output directory we specified
MODEL1_DIR=${MODEL1_OUTPUT}
if [ ! -d "$MODEL1_DIR" ]; then
    echo "Error: Could not find Model 1 evaluation results at ${MODEL1_DIR}"
    exit 1
fi
echo "✓ Model 1 results: ${MODEL1_DIR}"

# Generate Markdown report for Model 1
python ${SCRIPT_DIR}/generate_eval_report.py \
  ${MODEL1_DIR}/evaluation_report.json \
  --model "${MODEL1_NAME}" \
  --date $(date +%Y-%m-%d)
echo "✓ Model 1 Markdown report: ${MODEL1_DIR}/EVALUATION_REPORT.md"
echo ""
echo "Step 2: Evaluating Model 2 (${MODEL2_NAME}) with detailed match saving..."
echo "        Heading tolerance: ${HEADING_TOLERANCE}"
echo "--------------------------------------------------------------------------------"
MODEL2_OUTPUT="${OUTPUT_BASE}/${MODEL2_NAME}/${TIMESTAMP}"
python eval_tools/core/eval.py \
  --config ${MODEL2_CONFIG} \
  --output-dir ${MODEL2_OUTPUT} \
  --heading-tolerance ${HEADING_TOLERANCE} \
  --save-detailed-matches

# Use the output directory we specified
MODEL2_DIR=${MODEL2_OUTPUT}
if [ ! -d "$MODEL2_DIR" ]; then
    echo "Error: Could not find Model 2 evaluation results at ${MODEL2_DIR}"
    exit 1
fi
echo "✓ Model 2 results: ${MODEL2_DIR}"

# Generate Markdown report for Model 2
python ${SCRIPT_DIR}/generate_eval_report.py \
  ${MODEL2_DIR}/evaluation_report.json \
  --model "${MODEL2_NAME}" \
  --date $(date +%Y-%m-%d)
echo "✓ Model 2 Markdown report: ${MODEL2_DIR}/EVALUATION_REPORT.md"

# Step 3: Find common matches
echo ""
echo "Step 3: Finding common matches between the two models..."
echo "--------------------------------------------------------------------------------"
COMMON_MATCHES_DIR="${OUTPUT_BASE}/common_matches_${TIMESTAMP}"
mkdir -p ${COMMON_MATCHES_DIR}

python eval_tools/model_comparison/find_common_matches.py \
  --model1-matches ${MODEL1_DIR}/detailed_3d_matches.json \
  --model2-matches ${MODEL2_DIR}/detailed_3d_matches.json \
  --output ${COMMON_MATCHES_DIR}/common_matches.json \
  --model1-name "${MODEL1_NAME}" \
  --model2-name "${MODEL2_NAME}"

echo "✓ Common matches saved to: ${COMMON_MATCHES_DIR}/common_matches.json"

# Step 4: Compare models using common matches
echo ""
echo "Step 4: Comparing models using common matches only..."
echo "--------------------------------------------------------------------------------"
COMPARISON_DIR="${OUTPUT_BASE}/comparison_common_matches_${TIMESTAMP}"

python eval_tools/model_comparison/compare_models.py \
  --model1 ${MODEL1_DIR}/evaluation_report.json \
  --model2 ${MODEL2_DIR}/evaluation_report.json \
  --model1-name "${MODEL1_NAME}" \
  --model2-name "${MODEL2_NAME}" \
  --common-matches ${COMMON_MATCHES_DIR}/common_matches.json \
  --output-dir ${COMPARISON_DIR}

echo "✓ Comparison results saved to: ${COMPARISON_DIR}"

# Generate Markdown report for common-match comparison
python ${SCRIPT_DIR}/generate_comparison_report.py \
  ${COMPARISON_DIR}/comparison_report.json \
  --date $(date +%Y-%m-%d)
echo "✓ Markdown report: ${COMPARISON_DIR}/COMPARISON_REPORT.md"

# Step 5: Also run traditional comparison (without common match filtering)
echo ""
echo "Step 5: Running traditional comparison (all matches) for reference..."
echo "--------------------------------------------------------------------------------"
COMPARISON_TRADITIONAL_DIR="${OUTPUT_BASE}/comparison_all_matches_${TIMESTAMP}"

python eval_tools/model_comparison/compare_models.py \
  --model1 ${MODEL1_DIR}/evaluation_report.json \
  --model2 ${MODEL2_DIR}/evaluation_report.json \
  --model1-name "${MODEL1_NAME}" \
  --model2-name "${MODEL2_NAME}" \
  --output-dir ${COMPARISON_TRADITIONAL_DIR}

echo "✓ Traditional comparison saved to: ${COMPARISON_TRADITIONAL_DIR}"

# Generate Markdown report for traditional comparison
python ${SCRIPT_DIR}/generate_comparison_report.py \
  ${COMPARISON_TRADITIONAL_DIR}/comparison_report.json \
  --date $(date +%Y-%m-%d)
echo "✓ Markdown report: ${COMPARISON_TRADITIONAL_DIR}/COMPARISON_REPORT.md"

# Summary
echo ""
echo "================================================================================"
echo "WORKFLOW COMPLETE!"
echo "================================================================================"
echo ""
echo "Results Summary:"
echo "  Model 1 (${MODEL1_NAME}):"
echo "    - Evaluation: ${MODEL1_DIR}"
echo "    - Detailed matches: ${MODEL1_DIR}/detailed_3d_matches.json"
echo "    - Markdown report: ${MODEL1_DIR}/EVALUATION_REPORT.md"
echo ""
echo "  Model 2 (${MODEL2_NAME}):"
echo "    - Evaluation: ${MODEL2_DIR}"
echo "    - Detailed matches: ${MODEL2_DIR}/detailed_3d_matches.json"
echo "    - Markdown report: ${MODEL2_DIR}/EVALUATION_REPORT.md"
echo ""
echo "  Common Matches Analysis:"
echo "    - Common matches data: ${COMMON_MATCHES_DIR}/common_matches.json"
echo ""
echo "  Comparison Results:"
echo "    - Common matches only: ${COMPARISON_DIR}/"
echo "    - All matches (traditional): ${COMPARISON_TRADITIONAL_DIR}/"
echo ""
echo "Key Files to Review:"
echo "  1. ${COMPARISON_DIR}/comparison_report.txt"
echo "     (3D comparison based on common matches - fair comparison)"
echo "  1b. ${COMPARISON_DIR}/COMPARISON_REPORT.md"
echo "     (Markdown report - common matches)"
echo ""
echo "  2. ${COMPARISON_TRADITIONAL_DIR}/comparison_report.txt"
echo "     (Traditional comparison with all matches - for reference)"
echo "  2b. ${COMPARISON_TRADITIONAL_DIR}/COMPARISON_REPORT.md"
echo "     (Markdown report - all matches)"
echo ""
echo "  3. ${COMMON_MATCHES_DIR}/common_matches.json"
echo "     (Detailed statistics about match differences)"
echo ""
echo "To view the common-match comparison report:"
echo "  cat ${COMPARISON_DIR}/comparison_report.txt"
echo ""
