import os
import sys

root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
sys.path.append(root)

# ----------------------------------------------------------------------------

# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

# ----------------------------------------------------------------------------
# -*- coding: utf-8 -*-

from ccxt.test.exchange.base import test_shared_methods  # noqa E402

async def create_order_after_delay(exchange):
    await exchange.sleep(3000)
    await exchange.create_order('BTC/USDT:USDT', 'market', 'buy', 0.001)


async def test_un_watch_positions(exchange, skipped_properties, symbol):
    method = 'unWatchPositions'
    exchange.set_sandbox_mode(True)
    # First, we need to subscribe to positions to test the unsubscribe functionality
    positions_subscription = None
    try:
        # First call uses snapshot
        positions_subscription = await exchange.watch_positions()
        # trigger a position update
        exchange.spawn(create_order_after_delay, exchange)
        # Second call uses subscription
        positions_subscription = await exchange.watch_positions()
    except Exception as e:
        if not test_shared_methods.is_temporary_failure(e):
            raise e
        # If we can't subscribe, we can't test unsubscribe, so skip this test
        return False
    # Verify that we have a subscription
    assert isinstance(positions_subscription, list), exchange.id + ' ' + method + ' requires a valid positions subscription to test unsubscribe'
    # Assert unWatchPositions for one symbol is not supported
    error_response = None
    try:
        error_response = await exchange.un_watch_positions([symbol])
    except Exception as e:
        error_response = e
    assert error_response is not None, exchange.id + ' ' + method + ' must throw an error when unwatching a specific symbol, returned ' + exchange.json(error_response)
    # Test unwatching all positions (without specific symbols)
    response_all = None
    try:
        response_all = await exchange.un_watch_positions()
    except Exception as e:
        if not test_shared_methods.is_temporary_failure(e):
            raise e
        raise e
    # Verify the response for unwatching all positions
    assert response_all is not None, exchange.id + ' ' + method + ' must return a response when unwatching all positions, returned ' + exchange.json(response_all)
    # Test that we can resubscribe after unwatching (to ensure cleanup was proper)
    resubscribe_response = None
    try:
        resubscribe_response = await exchange.watch_positions()
        exchange.spawn(create_order_after_delay, exchange)
        resubscribe_response = await exchange.watch_positions()
    except Exception as e:
        if not test_shared_methods.is_temporary_failure(e):
            raise e
        raise Error(exchange.id + ' ' + method + ' failed to resubscribe after unwatch, indicating potential cleanup issues')
    # Verify resubscription works
    assert isinstance(resubscribe_response, list), exchange.id + ' ' + method + ' must allow resubscription after unwatch, returned ' + exchange.json(resubscribe_response)
    return True
