import assert from 'node:assert/strict';
import { ConnectorFactory } from '../src/connectors/factory.js';
import {
    normalizeOrderAction,
    normalizeOrderStatus,
    normalizeOrderType,
    normalizeTradeSide
} from '../src/domain/tradingEnums.js';
import { SymbolMapper } from '../src/utils/symbolMapper.js';

const assertTradingEnums = () => {
    assert.equal(normalizeTradeSide('SELL'), 'SELL');
    assert.equal(normalizeTradeSide('short'), 'SELL');
    assert.equal(normalizeTradeSide('BUY'), 'BUY');
    assert.equal(normalizeTradeSide('unknown'), 'BUY');

    assert.equal(normalizeOrderStatus('filled'), 'filled');
    assert.equal(normalizeOrderStatus('partially_filled'), 'partially_filled');
    assert.equal(normalizeOrderStatus('partial_fill'), 'partially_filled');
    assert.equal(normalizeOrderStatus('partiallyfilled'), 'partially_filled');
    assert.equal(normalizeOrderStatus('cancelled'), 'canceled');
    assert.equal(normalizeOrderStatus('canceled'), 'canceled');
    assert.equal(normalizeOrderStatus('expired'), 'expired');
    assert.equal(normalizeOrderStatus('rejected'), 'rejected');
    assert.equal(normalizeOrderStatus('unknown'), 'unknown');
    assert.equal(normalizeOrderStatus('new'), 'pending_new');

    assert.equal(normalizeOrderAction('entry'), 'ENTRY');
    assert.equal(normalizeOrderAction('EXIT'), 'EXIT');
    assert.equal(normalizeOrderAction('invalid'), undefined);
    assert.equal(normalizeOrderAction(undefined), undefined);

    assert.equal(normalizeOrderType('limit'), 'Limit');
    assert.equal(normalizeOrderType('stop'), 'Stop');
    assert.equal(normalizeOrderType('market'), 'Market');
    assert.equal(normalizeOrderType('other'), 'Market');
};

const assertSymbolMapper = () => {
    assert.equal(SymbolMapper.toTradeSymbol('BTC/USDT', 'alpaca'), 'BTC/USD');
    assert.equal(SymbolMapper.toTradeSymbol('BTC/USD', 'alpaca'), 'BTC/USD');
    assert.equal(SymbolMapper.toTradeSymbol('BTC/USDT', 'ccxt'), 'BTC/USDT');

    assert.equal(SymbolMapper.toDataSymbol('BTC/USD', 'alpaca'), 'BTC/USDT');
    assert.equal(SymbolMapper.toDataSymbol('BTC/USDT', 'ccxt'), 'BTC/USDT');
};

const assertConnectorFactory = () => {
    const alpacaConnector = ConnectorFactory.getCustomConnector('alpaca', 'key', 'secret');
    assert.equal(alpacaConnector.constructor.name, 'AlpacaConnector');

    const ccxtConnector = ConnectorFactory.getCustomConnector('ccxt', 'key', 'secret');
    assert.equal(ccxtConnector.constructor.name, 'CCXTConnector');

    assert.throws(
        () => ConnectorFactory.getCustomConnector('unsupported'),
        /is not supported/
    );
};

const main = () => {
    assertTradingEnums();
    assertSymbolMapper();
    assertConnectorFactory();
    console.log('[critical-coverage] PASS');
};

main();
