import requests
import json

BASE_URL = "http://localhost:18080"

print("=" * 80)
print("【修正後のAPI試算表テスト】")
print("=" * 80)
print()

try:
    # Get trial balance
    response = requests.get(f"{BASE_URL}/trial-balance")
    response.raise_for_status()
    
    data = response.json()
    
    # Find account 701
    accounts = data.get('accounts', [])
    account_701 = None
    for acc in accounts:
        if acc.get('account_code') == '701':
            account_701 = acc
            break
    
    if account_701:
        print(f"701 売上高（Sales Revenue）:")
        print(f"  借方（Debit）: {account_701.get('debit', 'N/A')}")
        print(f"  貸方（Credit）: {account_701.get('credit', 'N/A')}")
        print(f"  期末残高（Closing Balance）: {account_701.get('closing_balance', 'N/A')}")
        print()
    else:
        print("Account 701 not found!")
        print()
    
    # Show totals
    totals = data.get('totals', {})
    print("試算表合計（Trial Balance Totals）:")
    print(f"  借方合計: {totals.get('debit', 'N/A')}")
    print(f"  貸方合計: {totals.get('credit', 'N/A')}")
    print()
    
    # Check if debit = credit (this is the test)
    debit_total = float(totals.get('debit', 0) or 0)
    credit_total = float(totals.get('credit', 0) or 0)
    
    if debit_total == credit_total:
        print("✓ 試算表が正しくバランスしています（借方=貸方）")
    else:
        print(f"✗ 試算表がバランスしていません（借方={debit_total}, 貸方={credit_total}）")
    
except Exception as e:
    print(f"エラー: {e}")
    import traceback
    traceback.print_exc()
