import {test} from 'node:test';
import assert from 'node:assert';
import {claimGmxPriceImpactImpl, getClientForAddress} from '../../src/plugins/custom/gmx.js';

test('GMX Claim Price Impact', async (t) => {
    const testAccount = '0xbBA4eaA534cbD0EcAed5E2fD6036Aec2E7eE309f';

    await t.test('should claim price impact rebates for valid account with claimable fees', async () => {
        try {
            const sdk = await getClientForAddress(testAccount);
            const result = await claimGmxPriceImpactImpl(sdk);
            
            console.log('Claim price impact result:', result);
            
            // Should return a success indicator
            assert.ok(typeof result === 'string', 'Result should be a string');
            assert.equal(result, 'price_impact_claimed', 'Should return success indicator');
            
        } catch (error) {
            console.warn('Expected error in test environment:', error.message);
            assert.ok(error instanceof Error, 'Should throw an Error instance');
            
            // Should be a meaningful error message
            assert.ok(
                error.message.includes('No price impact fees available to claim') ||
                error.message.includes('Failed to claim price impact rebates') ||
                error.message.includes('Error claiming price impact rebates'),
                'Error message should be meaningful'
            );
        }
    });

    await t.test('should handle SDK client creation for claim', async () => {
        try {
            const sdk = await getClientForAddress(testAccount);
            
            // Validate SDK properties
            assert.ok(typeof sdk === 'object', 'SDK should be an object');
            assert.ok(sdk.account === testAccount, 'SDK account should match test account');
            assert.ok(typeof sdk.chainId === 'number', 'Chain ID should be a number');
            
            console.log('SDK validation passed for claim');
        } catch (error) {
            console.warn('Expected error in test environment:', error.message);
            assert.ok(error instanceof Error, 'Should throw an Error instance');
        }
    });

    await t.test('should automatically fetch claimable fees before claiming', async () => {
        try {
            const sdk = await getClientForAddress(testAccount);
            
            // The method should now automatically fetch claimable fees using GraphQL
            // This test verifies that the method integrates with getGmxPriceImpactRebatesImpl
            const result = await claimGmxPriceImpactImpl(sdk);
            
            console.log('Auto-fetch and claim result:', result);
            
            // If successful, should return the success indicator
            assert.equal(result, 'price_impact_claimed', 'Should return success indicator');
            
        } catch (error) {
            console.warn('Expected error in test environment:', error.message);
            assert.ok(error instanceof Error, 'Should throw an Error instance');
            
            // Should handle the case where no claimable fees are found
            if (error.message.includes('No price impact fees available to claim')) {
                console.log('No claimable fees found - this is expected for test account');
                assert.ok(true, 'Correctly handled no claimable fees case');
            } else {
                assert.ok(
                    error.message.includes('Failed to claim price impact rebates') ||
                    error.message.includes('Error claiming price impact rebates'),
                    'Should be a meaningful claim error'
                );
            }
        }
    });

    await t.test('should handle chain ID validation for claim', async () => {
        try {
            const sdk = await getClientForAddress(testAccount);
            
            // Should only work for Arbitrum (chainId 42161)
            assert.equal(sdk.chainId, 42161, 'Should be using Arbitrum chain ID');
            
            console.log('Chain ID validation passed for claim');
            
        } catch (error) {
            console.warn('Expected error in test environment:', error.message);
            assert.ok(error instanceof Error, 'Should throw an Error instance');
        }
    });

    await t.test('should validate contract addresses for claim', async () => {
        try {
            const sdk = await getClientForAddress(testAccount);
            
            // The method should validate that proper contract addresses are available
            // This tests the ExchangeRouter contract address retrieval
            await claimGmxPriceImpactImpl(sdk);
            
            console.log('Contract validation passed');
            
        } catch (error) {
            console.warn('Expected error in test environment:', error.message);
            assert.ok(error instanceof Error, 'Should throw an Error instance');
            
            // Validate error messages are meaningful
            assert.ok(
                error.message.includes('No price impact fees available to claim') ||
                error.message.includes('Failed to claim price impact rebates') ||
                error.message.includes('Unsupported chain ID') ||
                error.message.includes('Error claiming price impact rebates'),
                'Should provide meaningful error messages'
            );
        }
    });
}); 