import { describe, expect, it } from 'vitest';
import {
  DEFAULT_TRADE_PLANS_UI_STATE,
  reduceTradePlansUiState,
} from './tradePlansState';

describe('tradePlansState reducer', () => {
  it('applies a holding and switches the draft into sell management mode', () => {
    const next = reduceTradePlansUiState(DEFAULT_TRADE_PLANS_UI_STATE, {
      type: 'apply-holding',
      tradeId: 'TRD-1',
      symbol: 'AAPL',
      quantity: '10',
    });

    expect(next.selectedHoldingTradeId).toBe('TRD-1');
    expect(next.marketPriceSource).toBeNull();
    expect(next.draft.side).toBe('sell');
    expect(next.draft.symbol).toBe('AAPL');
    expect(next.draft.quantity).toBe('10');
    expect(next.draft.currentMarketPrice).toBe('');
  });

  it('resets the editor while preserving the current market price context', () => {
    const next = reduceTradePlansUiState({
      ...DEFAULT_TRADE_PLANS_UI_STATE,
      editingSetupId: 'setup-1',
      selectedHoldingTradeId: 'TRD-1',
      message: 'hello',
      error: 'bad',
      marketPriceSource: 'live',
      draft: {
        ...DEFAULT_TRADE_PLANS_UI_STATE.draft,
        symbol: 'BTC/USD',
        currentMarketPrice: '81234.0000',
        notes: 'keep',
      },
    }, {
      type: 'reset-form',
      currentMarketPrice: '81234.0000',
      marketPriceSource: 'live',
    });

    expect(next.editingSetupId).toBeNull();
    expect(next.selectedHoldingTradeId).toBeNull();
    expect(next.message).toBeNull();
    expect(next.error).toBeNull();
    expect(next.marketPriceSource).toBe('live');
    expect(next.draft.symbol).toBe('');
    expect(next.draft.currentMarketPrice).toBe('81234.0000');
    expect(next.draft.notes).toBe('');
  });
});
