#!/bin/bash

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 "Evaluation Workflow"
echo "================================================================================"

# Configuration
MODEL_CONFIG_ROI0="eval_tools/configs/eval_config_yolov26s-roi0.yaml"
MODEL_CONFIG_ROI1="eval_tools/configs/eval_config_yolov26s-roi1.yaml"
OUTPUT_BASE="evaluation_results/eval_results_yolo26s_768_20260407_DL_KPI_SCENE"  # Base directory for all evaluation outputs; individual runs will be timestamped subdirectories
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

MODEL_NAME="yolo26s-20260407-conf0.27"  # Used for output directory naming

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

# Step 1: Evaluate ROI0 detections
echo ""
echo "Step 1: Evaluating Model (${MODEL_NAME}) — ROI0 detections..."
echo "        Heading tolerance: ${HEADING_TOLERANCE}"
echo "--------------------------------------------------------------------------------"
MODEL_OUTPUT_ROI0="${OUTPUT_BASE}/${MODEL_NAME}/${TIMESTAMP}_roi0"
python eval_tools/core/eval.py \
  --config ${MODEL_CONFIG_ROI0} \
  --output-dir ${MODEL_OUTPUT_ROI0} \
  --heading-tolerance ${HEADING_TOLERANCE} \
  --save-detailed-matches

REPORT_JSON_ROI0="${MODEL_OUTPUT_ROI0}/evaluation_report.json"
REPORT_MD_ROI0="${MODEL_OUTPUT_ROI0}/EVALUATION_REPORT.md"
REPORT_DATE=$(date +%F)

echo ""
echo "Step 2: Generating Markdown report for ROI0..."
echo "--------------------------------------------------------------------------------"
python eval_tools/model_comparison/generate_eval_report.py \
  "${REPORT_JSON_ROI0}" \
  --output "${REPORT_MD_ROI0}" \
  --model "${MODEL_NAME}-roi0" \
  --date "${REPORT_DATE}"

# Step 3: Evaluate ROI1 detections
echo ""
echo "Step 3: Evaluating Model (${MODEL_NAME}) — ROI1 detections..."
echo "        Heading tolerance: ${HEADING_TOLERANCE}"
echo "--------------------------------------------------------------------------------"
MODEL_OUTPUT_ROI1="${OUTPUT_BASE}/${MODEL_NAME}/${TIMESTAMP}_roi1"
python eval_tools/core/eval.py \
  --config ${MODEL_CONFIG_ROI1} \
  --output-dir ${MODEL_OUTPUT_ROI1} \
  --heading-tolerance ${HEADING_TOLERANCE} \
  --save-detailed-matches

REPORT_JSON_ROI1="${MODEL_OUTPUT_ROI1}/evaluation_report.json"
REPORT_MD_ROI1="${MODEL_OUTPUT_ROI1}/EVALUATION_REPORT.md"

echo ""
echo "Step 4: Generating Markdown report for ROI1..."
echo "--------------------------------------------------------------------------------"
python eval_tools/model_comparison/generate_eval_report.py \
  "${REPORT_JSON_ROI1}" \
  --output "${REPORT_MD_ROI1}" \
  --model "${MODEL_NAME}-roi1" \
  --date "${REPORT_DATE}"

echo ""
echo "================================================================================"
echo "Evaluation completed"
echo "--------------------------------------------------------------------------------"
echo "ROI0 output:        ${MODEL_OUTPUT_ROI0}"
echo "ROI0 JSON report:   ${REPORT_JSON_ROI0}"
echo "ROI0 MD report:     ${REPORT_MD_ROI0}"
echo "ROI1 output:        ${MODEL_OUTPUT_ROI1}"
echo "ROI1 JSON report:   ${REPORT_JSON_ROI1}"
echo "ROI1 MD report:     ${REPORT_MD_ROI1}"
echo "================================================================================"
