#!/usr/bin/env python3
"""
Bond Sizing Analysis Tool
Analyzes optimal bond sizing for trustless bridge
"""

import json
import sys
from typing import Dict, List
from dataclasses import dataclass

@dataclass
class BondAnalysis:
    """Bond sizing analysis result"""
    deposit_amount: float
    current_bond: float
    bond_multiplier: float
    min_bond: float
    optimal_bond: float
    recommendation: str

def analyze_bond_sizing(
    deposit_amounts: List[float],
    bond_multiplier: float = 1.1,
    min_bond: float = 1.0,
    gas_price_eth: float = 0.0001,  # 100 gwei in ETH
    attack_cost_multiplier: float = 1.2  # Attack should cost 20% more than profit
) -> List[BondAnalysis]:
    """
    Analyze bond sizing for various deposit amounts
    
    Args:
        deposit_amounts: List of deposit amounts in ETH
        bond_multiplier: Current bond multiplier (default 1.1 = 110%)
        min_bond: Minimum bond amount in ETH
        gas_price_eth: Gas price in ETH (for attack cost calculation)
        attack_cost_multiplier: Multiplier for attack cost vs profit
    
    Returns:
        List of bond analysis results
    """
    results = []
    
    for deposit in deposit_amounts:
        # Current bond calculation
        current_bond = max(deposit * bond_multiplier, min_bond)
        
        # Attack cost (gas for fraudulent claim + bond)
        attack_gas_cost = 0.001 * gas_price_eth  # Estimate 1M gas
        attack_total_cost = current_bond + attack_gas_cost
        
        # Profit from successful fraud
        fraud_profit = deposit
        
        # Optimal bond should make attack unprofitable
        # attack_cost >= fraud_profit * attack_cost_multiplier
        optimal_bond = (fraud_profit * attack_cost_multiplier) - attack_gas_cost
        optimal_bond = max(optimal_bond, min_bond)
        
        # Recommendation
        if current_bond >= optimal_bond:
            recommendation = "Current bond is sufficient"
        elif current_bond < optimal_bond * 0.9:
            recommendation = f"Consider increasing bond to {optimal_bond:.2f} ETH"
        else:
            recommendation = "Current bond is close to optimal"
        
        results.append(BondAnalysis(
            deposit_amount=deposit,
            current_bond=current_bond,
            bond_multiplier=bond_multiplier,
            min_bond=min_bond,
            optimal_bond=optimal_bond,
            recommendation=recommendation
        ))
    
    return results

def print_analysis(results: List[BondAnalysis]):
    """Print bond analysis results"""
    print("=" * 80)
    print("Bond Sizing Analysis")
    print("=" * 80)
    print(f"{'Deposit':<12} {'Current Bond':<15} {'Optimal Bond':<15} {'Recommendation':<30}")
    print("-" * 80)
    
    for result in results:
        print(f"{result.deposit_amount:>10.2f} ETH  "
              f"{result.current_bond:>12.2f} ETH  "
              f"{result.optimal_bond:>12.2f} ETH  "
              f"{result.recommendation:<30}")
    
    print("=" * 80)

def main():
    """Main entry point"""
    # Example deposit amounts to analyze
    deposit_amounts = [0.1, 0.5, 1.0, 5.0, 10.0, 50.0, 100.0]
    
    # Analyze bond sizing
    results = analyze_bond_sizing(deposit_amounts)
    
    # Print results
    print_analysis(results)
    
    # Optional: Export to JSON
    if len(sys.argv) > 1 and sys.argv[1] == '--json':
        output = {
            'analysis': [
                {
                    'deposit_amount': r.deposit_amount,
                    'current_bond': r.current_bond,
                    'optimal_bond': r.optimal_bond,
                    'recommendation': r.recommendation
                }
                for r in results
            ]
        }
        print(json.dumps(output, indent=2))

if __name__ == '__main__':
    main()

