#!/usr/bin/env python3
"""Verify the print modification is correct"""
import requests

print("🔍 Verifying print modifications...\n")

# Check HTML
print("📄 Checking HTML...")
html_response = requests.get("http://127.0.0.1:18080/trial-balance.html")
html = html_response.text

html_checks = [
    ("First page header preserved", '<h2>残高試算表（貸借・損益）</h2>', True),
    ("Period info preserved", 'id="periodInfo"', True),
    ("Print date div preserved", 'id="printDate"', True),
    ("Old printHeaderRow removed", 'id="printHeaderRow"', False),
    ("Original @page structure", '@page {', True),
]

html_pass = 0
for check_name, needle, should_exist in html_checks:
    found = needle in html
    status = "✓" if found == should_exist else "✗"
    result = "found" if found else "not found"
    expected = "should exist" if should_exist else "should NOT exist"
    
    if found == should_exist:
        html_pass += 1
    print(f"  {status} {check_name}: {result} ({expected})")

# Check JavaScript
print("\n📝 Checking JavaScript...")
js_response = requests.get("http://127.0.0.1:18080/js/trial-balance.js")
js = js_response.text

js_checks = [
    ("insertPageHeaders function", "function insertPageHeaders", True),
    ("removePageHeaders function", "function removePageHeaders", True),
    ("insertPageHeaders called in print", "insertPageHeaders(dateFrom, dateTo)", True),
    ("removePageHeaders called after print", "removePageHeaders()", True),
    ("Old setupPrintHeaderInfo removed", "function setupPrintHeaderInfo", False),
    ("Old formatPeriodInfo removed", "function formatPeriodInfo", False),
]

js_pass = 0
for check_name, needle, should_exist in js_checks:
    found = needle in js
    status = "✓" if found == should_exist else "✗"
    result = "found" if found else "not found"
    expected = "should exist" if should_exist else "should NOT exist"
    
    if found == should_exist:
        js_pass += 1
    print(f"  {status} {check_name}: {result} ({expected})")

# Summary
print(f"\n✅ HTML checks: {html_pass}/{len(html_checks)} passed")
print(f"✅ JS checks: {js_pass}/{len(js_checks)} passed")

if html_pass == len(html_checks) and js_pass == len(js_checks):
    print("\n🎉 All modifications verified successfully!")
    print("📌 Summary of changes:")
    print("   - Reverted printHeaderRow from HTML")
    print("   - Restored original @page structure")
    print("   - Added insertPageHeaders() for dynamic page headers")
    print("   - Added removePageHeaders() to clean up after printing")
    print("   - First page remains unchanged")
    print("   - Each printed page will show title, period, date, and unit")
else:
    print("\n⚠️  Some checks failed. Please review the modifications.")
