"""
Example 1: Basic Payment

Demonstrates:
- Creating a Route402 client
- Making a simple USDC payment
- Checking payment status
"""
from route402 import Route402Client, PaymentStatus

# Initialize client with your API key
# Get API key from: https://route402.com
client = Route402Client(api_key="apk_your_key_here")

# Create a payment
print("Creating payment...")
payment = client.create_payment(
    seller="0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",  # Seller's address
    amount="0.10",  # 10 cents in USDC
    reference="example-payment-1"  # Optional tracking reference
)

print(f"\n✅ Payment created!")
print(f"Payment ID: {payment.id}")
print(f"Status: {payment.status.value}")
print(f"Amount: {payment.amount} USDC")
print(f"Fee: {payment.fee_amount} USDC (0.5%)")
print(f"Net to seller: {payment.net_amount} USDC")

if payment.tx_hash:
    print(f"\n📊 View on BaseScan:")
    print(f"https://basescan.org/tx/{payment.tx_hash}")

# Wait for confirmation (if not already confirmed)
if not payment.is_confirmed:
    print("\n⏳ Waiting for confirmation (takes ~25 seconds)...")
    import time
    while not payment.is_confirmed:
        time.sleep(5)
        payment = client.get_payment(payment.id)
        print(f"Status: {payment.status.value}")

    print("\n✅ Payment confirmed on blockchain!")
    print(f"Confirmed at: {payment.confirmed_at}")
else:
    print("\n✅ Payment already confirmed!")

# Export payments to CSV (for accounting)
print("\n📊 Exporting payments to CSV...")
csv_data = client.export_payments_csv()

with open("payments.csv", "w") as f:
    f.write(csv_data)

print("✅ Exported to payments.csv")
