import { describe, expect, it } from 'vitest';
import {
    aggregateCanonicalLifecycleTrades,
    buildCanonicalLifecycleTrades,
    type LifecycleOrderRow
} from './orderLifecycleLedger';

describe('orderLifecycleLedger', () => {
    it('builds canonical realized trades from lifecycle orders', () => {
        const rows: LifecycleOrderRow[] = [
            {
                order_id: '11111111-1111-1111-1111-111111111111',
                profile_id: 'profile-1',
                trade_id: 'TRD-1',
                symbol: 'BTC/USDT',
                action: 'ENTRY',
                side: 'BUY',
                qty: 0.1,
                price: 50000,
                status: 'filled',
                source: 'BOT',
                created_at: '2026-02-21T10:00:00Z'
            },
            {
                order_id: '22222222-2222-2222-2222-222222222222',
                profile_id: 'profile-1',
                trade_id: 'TRD-1',
                symbol: 'BTC/USDT',
                action: 'EXIT',
                side: 'SELL',
                qty: 0.1,
                price: 51000,
                status: 'filled',
                source: 'BOT',
                created_at: '2026-02-21T10:05:00Z'
            }
        ];

        const trades = buildCanonicalLifecycleTrades(rows);
        expect(trades).toHaveLength(1);
        expect(trades[0].tradeId).toBe('TRD-1');
        expect(trades[0].pnl).toBeCloseTo(100, 8);
        expect(trades[0].pnlPercent).toBeCloseTo(2, 8);
    });

    it('excludes synthetic backfill/manual order ids from canonical realized trades', () => {
        const rows: LifecycleOrderRow[] = [
            {
                order_id: 'BFILL-abc123',
                profile_id: 'profile-1',
                trade_id: 'TRD-SYNTH',
                symbol: 'SOL/USDT',
                action: 'EXIT',
                side: 'SELL',
                qty: 1,
                price: 100,
                status: 'filled',
                source: 'BOT',
                created_at: '2026-02-21T10:05:00Z'
            },
            {
                order_id: 'MANOVR-abc123',
                profile_id: 'profile-1',
                trade_id: 'TRD-SYNTH',
                symbol: 'SOL/USDT',
                action: 'ENTRY',
                side: 'BUY',
                qty: 1,
                price: 90,
                status: 'filled',
                source: 'BOT',
                created_at: '2026-02-21T10:00:00Z'
            }
        ];

        expect(buildCanonicalLifecycleTrades(rows)).toHaveLength(0);
    });

    it('aggregates totals and profile win rate from canonical trades', () => {
        const agg = aggregateCanonicalLifecycleTrades([
            {
                id: 'canonical:profile-1:TRD-1',
                tradeId: 'TRD-1',
                profileId: 'profile-1',
                symbol: 'BTC/USDT',
                side: 'BUY',
                size: 1,
                entryPrice: 100,
                exitPrice: 110,
                pnl: 10,
                pnlPercent: 10,
                closedAtMs: 1700000000000
            },
            {
                id: 'canonical:profile-1:TRD-2',
                tradeId: 'TRD-2',
                profileId: 'profile-1',
                symbol: 'BTC/USDT',
                side: 'BUY',
                size: 1,
                entryPrice: 100,
                exitPrice: 90,
                pnl: -10,
                pnlPercent: -10,
                closedAtMs: 1700000001000
            }
        ]);

        expect(agg.tradeCount).toBe(2);
        expect(agg.wins).toBe(1);
        expect(agg.winRate).toBeCloseTo(50, 8);
        expect(agg.totalPnl).toBeCloseTo(0, 8);
        expect(agg.byProfile['profile-1'].tradeCount).toBe(2);
        expect(agg.byProfile['profile-1'].realizedPnl).toBeCloseTo(0, 8);
    });
});

