import {
  CashFlowPanelData,
  Company,
  DividendsPanelData,
  EarningsPanelData,
  FinancialsPanelData,
  Portfolio,
  StockAnalysis,
} from './financial';
import { TaskProfile } from './agentSettings';
import { NewsArticle } from './news';

export type PanelPayload =
  | { type: 'company'; data: Company }
  | { type: 'error'; data: ErrorPanel }
  | { type: 'portfolio'; data: Portfolio }
  | { type: 'news'; data: NewsArticle[]; ticker?: string }
  | { type: 'analysis'; data: StockAnalysis }
  | { type: 'financials'; data: FinancialsPanelData }
  | { type: 'cashFlow'; data: CashFlowPanelData }
  | { type: 'dividends'; data: DividendsPanelData }
  | { type: 'earnings'; data: EarningsPanelData };

export type TerminalCommandResponse =
  | { kind: 'text'; content: string; portfolio?: Portfolio }
  | { kind: 'panel'; panel: PanelPayload };

export interface ChatPanelContext {
  sourceCommand?: string;
  capturedAt?: string;
  panel: PanelPayload;
}

export interface ExecuteTerminalCommandRequest {
  workspaceId: string;
  input: string;
}

export interface LookupCompanyRequest {
  symbol: string;
}

export interface StartChatStreamRequest {
  requestId: string;
  workspaceId: string;
  sessionId?: string;
  prompt: string;
  agentProfile?: TaskProfile;
  modelOverride?: string;
  panelContext?: ChatPanelContext;
}

export interface ChatStreamStart {
  requestId: string;
  sessionId: string;
}

export interface ResolveAgentToolApprovalRequest {
  approvalId: string;
  approved: boolean;
}

export type AgentStreamItemKind =
  | 'reasoning_delta'
  | 'text_delta'
  | 'tool_command'
  | 'tool_result'
  | 'approval_required'
  | 'stream_complete'
  | 'error';

export interface AgentStreamItemEvent {
  workspaceId: string;
  requestId: string;
  sessionId: string;
  sequence: number;
  kind: AgentStreamItemKind;
  delta?: string;
  command?: string;
  response?: TerminalCommandResponse;
  approvalId?: string;
  title?: string;
  message?: string;
  errorMessage?: string;
}

export interface TerminalEntry {
  id: string;
  type: 'command' | 'response' | 'system' | 'error' | 'panel' | 'thinking';
  content: string | PanelPayload;
  renderMode?: 'plain' | 'markdown';
  timestamp?: Date;
}

export interface ErrorPanel {
  title: string;
  message: string;
  detail?: string;
  provider?: string;
  query?: string;
  symbol?: string;
}

export interface CommandSuggestion {
  command: string;
  description: string;
  category:
    | 'search'
    | 'financials'
    | 'cashflow'
    | 'dividends'
    | 'earnings'
    | 'mappings'
    | 'portfolio'
    | 'cash'
    | 'news'
    | 'analysis'
    | 'research'
    | 'system';
}

export type PortfolioAction =
  | 'overview'
  | 'stats'
  | 'history'
  | 'buy'
  | 'sell'
  | 'deposit'
  | 'withdraw';

export interface PortfolioActionDraft {
  symbol: string;
  quantity: string;
  price: string;
  amount: string;
}

export interface PortfolioActionSeed {
  symbol?: string;
  quantity?: string;
  price?: string;
  amount?: string;
}

export interface TerminalState {
  history: TerminalEntry[];
  currentIndex: number;
  isProcessing: boolean;
}

export interface TickerHistorySnapshot {
  symbol: string;
  name: string;
  price?: number;
  change?: number;
  changePercent?: number;
  volume?: number;
  marketCap?: number;
}

export interface TickerHistoryEntry {
  company: TickerHistorySnapshot;
  timestamp: Date;
  isLoading: boolean;
}
