#!/usr/bin/env python3
"""
Test getPositionsV2 to list all positions for our wallet
"""
import sys
import os
from pathlib import Path
from dotenv import load_dotenv

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))

from apollox_v2_client import ApolloXV2Client

# Load environment variables
load_dotenv()

# Read wallet private key from .env
private_key = os.getenv('WALLET_PRIVATE_KEY')
if not private_key:
    raise ValueError("WALLET_PRIVATE_KEY not found in .env")

print("="*80)
print("🔍 TEST GET ALL POSITIONS FOR WALLET")
print("="*80)

# Initialize client
client = ApolloXV2Client(private_key=private_key)

print(f"\n✅ Client initialized")
print(f"   Wallet: {client.account.address}")

# Call getPositionsV2
try:
    print(f"\n🔍 Calling getPositionsV2({client.account.address})...")

    positions = client.contract.functions.getPositionsV2(client.account.address).call()

    print(f"\n📊 Found {len(positions)} position(s):")

    for i, pos_hash in enumerate(positions):
        print(f"\n   {i+1}. 0x{pos_hash.hex()}")

    if len(positions) == 0:
        print("\n⚠️  No open positions found on-chain for this wallet")
        print("   This means the position we're trying to close doesn't exist!")

except Exception as e:
    print(f"\n❌ Error calling getPositionsV2: {e}")

print("\n" + "="*80)
