#!/usr/bin/env python3
"""Update report HTML with fixed reference section + curated sector refs.
Run this with: flask shell < update_references.py"""

code = '''
from app.ai.report_generator import generate_report_html
from app.extensions import sanitize_html
from app.ai.diamond import _collect_references

reports = Report.query.filter(Report.synthesis_result.isnot(None)).all()
print(f"Found {len(reports)} reports to update")

updated_count = 0
for report in reports:
    market = report.market_analysis or {}
    financial = report.financial_analysis or {}
    competitive = report.competitive_analysis or {}
    synthesis = report.synthesis_result or {}
    refs = _collect_references(market, financial, competitive, synthesis,
                               report.project_name, report.project_description)
    report.report_references = refs
    new_html = generate_report_html(report.synthesis_result, report.language, refs)
    report.full_report_html = sanitize_html(new_html)
    print(f"Updated report {report.id}: {report.project_name[:50]}...")
    updated_count += 1

db.session.commit()
print(f"\\n{updated_count} reports updated successfully!")
'''

exec(code)
