import { describe, expect, it } from 'vitest';
import { buildSimpleSetupPayload } from './SimpleView';

describe('SimpleView helpers', () => {
  it('builds a buy dip setup payload with dollar drop and percent profit target', () => {
    const payload = buildSimpleSetupPayload({
      draft: {
        symbol: 'aapl',
        side: 'buy',
        sizingMode: 'quantity',
        quantity: '5',
        amountUsd: '',
        currentMarketPrice: '210.25',
        dropMode: 'dollar',
        dropValue: '12',
        profitMode: 'percent',
        profitValue: '8',
        notes: 'Long-term compounder',
      },
    });

    expect(payload).toEqual({
      stock_instance_id: undefined,
      symbol: 'AAPL',
      active: true,
      status: 'simple_armed_buy',
      is_crypto: false,
      is_real_trade: false,
      label: 'Simple',
      quantity: 5,
      amount_usd: null,
      sizing_mode: 'quantity',
      filled_quantity: null,
      notes: 'Long-term compounder',
      entry_price: null,
      reference_price: 210.25,
      gain_threshold_for_sell: 8,
      drop_threshold_for_buy: 12,
      workflow_type: 'simple',
      simple_side: 'buy',
      drop_trigger_mode: 'dollar',
      profit_target_mode: 'percent',
      linked_trade_id: null,
      profile_id: null,
      holding_mode: 'short_term',
      automation_state: 'armed',
      buy_price: null,
      sell_price: null,
      buy_time: null,
      sell_time: null,
    });
  });

  it('builds a sell setup payload from an existing holding', () => {
    const payload = buildSimpleSetupPayload({
      draft: {
        symbol: 'msft',
        side: 'sell',
        sizingMode: 'quantity',
        quantity: '1',
        amountUsd: '',
        currentMarketPrice: '420.50',
        dropMode: 'percent',
        dropValue: '',
        profitMode: 'dollar',
        profitValue: '15',
        notes: '',
      },
      holding: {
        symbol: 'MSFT',
        size: 10,
        entryPrice: 380.25,
        profileId: 'simple-profile',
        tradeId: 'TRD-123',
      },
    });

    expect(payload).toEqual({
      stock_instance_id: undefined,
      symbol: 'MSFT',
      active: true,
      status: 'simple_armed_sell',
      is_crypto: false,
      is_real_trade: false,
      label: 'Simple',
      quantity: 10,
      amount_usd: null,
      sizing_mode: 'quantity',
      filled_quantity: 10,
      notes: null,
      entry_price: 380.25,
      reference_price: 420.5,
      gain_threshold_for_sell: 15,
      drop_threshold_for_buy: null,
      workflow_type: 'simple',
      simple_side: 'sell',
      drop_trigger_mode: null,
      profit_target_mode: 'dollar',
      linked_trade_id: 'TRD-123',
      profile_id: 'simple-profile',
      holding_mode: 'short_term',
      automation_state: 'armed',
      buy_price: null,
      sell_price: null,
      buy_time: null,
      sell_time: null,
    });
  });

  it('builds an amount-sized buy setup payload', () => {
    const payload = buildSimpleSetupPayload({
      draft: {
        symbol: 'btc/usd',
        side: 'buy',
        sizingMode: 'amount',
        quantity: '',
        amountUsd: '250',
        currentMarketPrice: '81366.105',
        dropMode: 'percent',
        dropValue: '0',
        profitMode: 'dollar',
        profitValue: '25',
        notes: '',
      },
    });

    expect(payload).toEqual({
      stock_instance_id: undefined,
      symbol: 'BTC/USD',
      active: true,
      status: 'simple_armed_buy',
      is_crypto: false,
      is_real_trade: false,
      label: 'Simple',
      quantity: null,
      amount_usd: 250,
      sizing_mode: 'amount',
      filled_quantity: null,
      notes: null,
      entry_price: null,
      reference_price: 81366.105,
      gain_threshold_for_sell: 25,
      drop_threshold_for_buy: 0,
      workflow_type: 'simple',
      simple_side: 'buy',
      drop_trigger_mode: 'percent',
      profit_target_mode: 'dollar',
      linked_trade_id: null,
      profile_id: null,
      holding_mode: 'short_term',
      automation_state: 'armed',
      buy_price: null,
      sell_price: null,
      buy_time: null,
      sell_time: null,
    });
  });

  it('preserves long-term mode when editing an existing setup', () => {
    const payload = buildSimpleSetupPayload({
      draft: {
        symbol: 'aapl',
        side: 'buy',
        sizingMode: 'quantity',
        quantity: '5',
        amountUsd: '',
        currentMarketPrice: '210.25',
        dropMode: 'dollar',
        dropValue: '12',
        profitMode: 'percent',
        profitValue: '8',
        notes: 'Long-term compounder',
      },
      existingId: 'simple-1',
      existingEntry: {
        stock_instance_id: 'simple-1',
        symbol: 'AAPL',
        active: true,
        status: 'simple_bought',
        is_crypto: false,
        is_real_trade: false,
        workflow_type: 'simple',
        simple_side: 'buy',
        holding_mode: 'long_term',
        automation_state: 'paused_long_term',
      } as any,
    });

    expect(payload.holding_mode).toBe('long_term');
    expect(payload.automation_state).toBe('paused_long_term');
  });

  it('rejects sell setups without an existing holding', () => {
    expect(() => buildSimpleSetupPayload({
      draft: {
        symbol: 'nvda',
        side: 'sell',
        sizingMode: 'quantity',
        quantity: '5',
        amountUsd: '',
        currentMarketPrice: '900',
        dropMode: 'percent',
        dropValue: '',
        profitMode: 'percent',
        profitValue: '6',
        notes: '',
      },
    })).toThrow('Sell setups require an existing holding for this symbol.');
  });
});
