#!/usr/bin/env python3
"""
Debug: affiche les raw data de The Graph
"""

import requests
import json

WALLET_ADDRESS = "0x7476737c49d55bea691e54c2c96fd39a6baf3049"
THEGRAPH_URL = "https://gateway-arbitrum.network.thegraph.com/api/6edc89d15e0bffdb56057765735dfdc8/subgraphs/id/HxvztN7GmfYYDPb89K4FFYKFkRvBtscxYQqvX79D4XSQ"

GRAPHQL_QUERY = """
query OrderAndTradeHistorys($skip: Int, $first: Int, $user: String) {
    orderAndTradeHistories(
        skip: $skip
        first: $first
        orderBy: timestamp
        orderDirection: desc
        where: {
            user: $user
            actionType_not: ADD_MARGIN
        }
    ) {
        id
        orderOrTradeHash
        actionType
        timestamp
        txHash
        trade {
            pair {
                name
            }
            isLong
            token {
                symbol
            }
            margin
            entryPrice
            qty
            openFee
            executionFee
            closeType
            closePrice
            closeFee
            fundingFee
            holdingFee
            pnl
            actualPnl
        }
    }
}
"""

variables = {
    "user": WALLET_ADDRESS,
    "skip": 0,
    "first": 10
}

response = requests.post(
    THEGRAPH_URL,
    json={
        'query': GRAPHQL_QUERY,
        'variables': variables
    },
    headers={
        'Content-Type': 'application/json',
        'Accept': '*/*',
        'Origin': 'https://www.apollox.finance',
        'Referer': 'https://www.apollox.finance/',
        'User-Agent': 'Mozilla/5.0'
    },
    timeout=30
)

data = response.json()

print("=" * 80)
print("RAW RESPONSE FROM THE GRAPH")
print("=" * 80)
print()
print(json.dumps(data, indent=2))
