export type SimpleSide = 'buy' | 'sell';
export type TriggerMode = 'dollar' | 'percent';
export type MarketPriceSource = 'live' | 'latest_close' | 'reference_price' | null;

export type SimpleSetupDraft = {
  symbol: string;
  side: SimpleSide;
  sizingMode: 'quantity' | 'amount';
  quantity: string;
  amountUsd: string;
  currentMarketPrice: string;
  dropMode: TriggerMode;
  dropValue: string;
  profitMode: TriggerMode;
  profitValue: string;
  notes: string;
};

export const DEFAULT_TRADE_PLAN_DRAFT: SimpleSetupDraft = {
  symbol: '',
  side: 'buy',
  sizingMode: 'quantity',
  quantity: '',
  amountUsd: '',
  currentMarketPrice: '',
  dropMode: 'percent',
  dropValue: '',
  profitMode: 'percent',
  profitValue: '',
  notes: '',
};

export type TradePlansUiState = {
  editingSetupId: string | null;
  draft: SimpleSetupDraft;
  marketPriceSource: MarketPriceSource;
  copiedKey: string | null;
  message: string | null;
  error: string | null;
  selectedHoldingTradeId: string | null;
  focusedSetupId: string | null;
};

export const DEFAULT_TRADE_PLANS_UI_STATE: TradePlansUiState = {
  editingSetupId: null,
  draft: DEFAULT_TRADE_PLAN_DRAFT,
  marketPriceSource: null,
  copiedKey: null,
  message: null,
  error: null,
  selectedHoldingTradeId: null,
  focusedSetupId: null,
};

export type TradePlansUiAction =
  | { type: 'set-draft-field'; key: keyof SimpleSetupDraft; value: SimpleSetupDraft[keyof SimpleSetupDraft] }
  | { type: 'replace-draft'; draft: SimpleSetupDraft }
  | { type: 'set-editing-setup-id'; value: string | null }
  | { type: 'set-market-price-source'; value: MarketPriceSource }
  | { type: 'set-copied-key'; value: string | null }
  | { type: 'set-message'; value: string | null }
  | { type: 'set-error'; value: string | null }
  | { type: 'set-selected-holding-trade-id'; value: string | null }
  | { type: 'set-focused-setup-id'; value: string | null }
  | { type: 'clear-feedback' }
  | { type: 'reset-form'; currentMarketPrice: string; marketPriceSource: MarketPriceSource }
  | { type: 'apply-holding'; tradeId: string | null; symbol: string; quantity: string };

export function reduceTradePlansUiState(state: TradePlansUiState, action: TradePlansUiAction): TradePlansUiState {
  switch (action.type) {
    case 'set-draft-field':
      return {
        ...state,
        draft: {
          ...state.draft,
          [action.key]: action.value,
        },
      };
    case 'replace-draft':
      return {
        ...state,
        draft: action.draft,
      };
    case 'set-editing-setup-id':
      return {
        ...state,
        editingSetupId: action.value,
      };
    case 'set-market-price-source':
      return {
        ...state,
        marketPriceSource: action.value,
      };
    case 'set-copied-key':
      return {
        ...state,
        copiedKey: action.value,
      };
    case 'set-message':
      return {
        ...state,
        message: action.value,
      };
    case 'set-error':
      return {
        ...state,
        error: action.value,
      };
    case 'set-selected-holding-trade-id':
      return {
        ...state,
        selectedHoldingTradeId: action.value,
      };
    case 'set-focused-setup-id':
      return {
        ...state,
        focusedSetupId: action.value,
      };
    case 'clear-feedback':
      return {
        ...state,
        message: null,
        error: null,
      };
    case 'reset-form':
      return {
        ...state,
        editingSetupId: null,
        selectedHoldingTradeId: null,
        message: null,
        error: null,
        marketPriceSource: action.marketPriceSource,
        draft: {
          ...DEFAULT_TRADE_PLAN_DRAFT,
          currentMarketPrice: action.currentMarketPrice,
        },
      };
    case 'apply-holding':
      return {
        ...state,
        selectedHoldingTradeId: action.tradeId,
        marketPriceSource: null,
        draft: {
          ...state.draft,
          side: 'sell',
          symbol: action.symbol,
          quantity: action.quantity,
          currentMarketPrice: '',
        },
      };
    default:
      return state;
  }
}
