import os
import json
from jinja2 import Environment, FileSystemLoader

# --- Configuration ---
OUTPUT_DIR = "output"
REPORT_DATA_PATH = os.path.join(OUTPUT_DIR, 'report_data.json')
TEMPLATE_PATH = 'template.html'
OUTPUT_HTML_PATH = os.path.join(OUTPUT_DIR, 'report.html')

def regenerate_report():
    """
    Quickly regenerates the HTML report from existing data without
    re-running the model inference.
    """
    print("Regenerating HTML report from existing data...")

    # Check if the data file exists
    if not os.path.exists(REPORT_DATA_PATH):
        print(f"Error: Data file not found at '{REPORT_DATA_PATH}'.")
        print("Please run 'run_evaluation.py' first to generate the data.")
        return

    # Load the report data from the JSON file
    with open(REPORT_DATA_PATH, 'r', encoding='utf-8') as f:
        report_data = json.load(f)

    # Load the Jinja2 template
    env = Environment(loader=FileSystemLoader('.'))
    template = env.get_template(TEMPLATE_PATH)

    # Render the template with the loaded data
    html_content = template.render(
        models=report_data.get('models', []),
        images=report_data.get('images', [])
    )

    # Write the new HTML file
    with open(OUTPUT_HTML_PATH, 'w', encoding='utf-8') as f:
        f.write(html_content)

    print(f"Done! Report regenerated at '{os.path.abspath(OUTPUT_HTML_PATH)}'")

if __name__ == '__main__':
    regenerate_report() 