import { Annotation } from '@langchain/langgraph';
import { BaseWorkflowState } from '../base-workflow.js';

/**
 * Trading request workflow state
 *
 * Handles human-in-the-loop approval for trade execution
 */
export const TradingRequestState = Annotation.Root({
  ...BaseWorkflowState.spec,

  // Input
  requestType: Annotation<'market_order' | 'limit_order' | 'stop_loss'>(),
  ticker: Annotation<string>(),
  side: Annotation<'buy' | 'sell'>(),
  amount: Annotation<number>(), // Requested amount
  price: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),

  // Analysis step
  marketAnalysis: Annotation<string | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  currentPrice: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),

  // Risk calculation
  riskAssessment: Annotation<Record<string, unknown> | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  riskRewardRatio: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  positionSize: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),

  // Human approval
  approvalRequested: Annotation<boolean>({
    value: (left, right) => right ?? left,
    default: () => false,
  }),
  userApproved: Annotation<boolean>({
    value: (left, right) => right ?? left,
    default: () => false,
  }),
  approvalMessage: Annotation<string | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  approvalTimestamp: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),

  // Execution
  orderPlaced: Annotation<boolean>({
    value: (left, right) => right ?? left,
    default: () => false,
  }),
  orderId: Annotation<string | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  executionPrice: Annotation<number | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
  executionStatus: Annotation<'pending' | 'filled' | 'rejected' | 'cancelled' | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),

  // Output
  summary: Annotation<string | null>({
    value: (left, right) => right ?? left,
    default: () => null,
  }),
});

export type TradingRequestStateType = typeof TradingRequestState.State;
