import {
  BalanceSheetPanelData,
  CashFlowPanelData,
  Company,
  DividendsPanelData,
  EarningsPanelData,
  FilingsPanelData,
  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: 'balanceSheet'; data: BalanceSheetPanelData }
  | { type: 'cashFlow'; data: CashFlowPanelData }
  | { type: 'dividends'; data: DividendsPanelData }
  | { type: 'earnings'; data: EarningsPanelData }
  | { type: 'filings'; data: FilingsPanelData };

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 CancelChatStreamRequest {
  workspaceId: string;
  requestId: 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 interface PendingAgentApproval {
  approvalId: string;
  command: string;
  requestId: string;
  workspaceId: string;
  title: string;
  message: string;
}

export interface TerminalRequestState {
  kind: 'slash' | 'chat';
  requestId: string;
}

export type AgentStreamItemKind =
  | 'reasoning_delta'
  | 'text_delta'
  | 'tool_command'
  | 'tool_result'
  | 'approval_required'
  | 'stream_complete'
  | 'cancelled'
  | '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 WorkspaceTerminalState {
  history: TerminalEntry[];
  chatSessionId?: string;
  inputDraft: string;
  isProcessing: boolean;
  activeRequest: TerminalRequestState | null;
  pendingApproval: PendingAgentApproval | null;
  scrollTop: number;
  stickToBottom: boolean;
  collapsedThinkingEntryIds: string[];
}

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;
}

export type ChatHistoryRestoreTarget = 'activeTab' | 'newTab';

export interface ChatHistoryEntry {
  id: string;
  title: string;
  prompt: string;
  transcript: TerminalEntry[];
  createdAt: Date;
  updatedAt: Date;
  commandCount: number;
  responsePreview?: string;
}

export interface WorkspacePreferences {
  chatHistoryRestoreTarget: ChatHistoryRestoreTarget;
}

export type BackgroundJobKind =
  | 'news_scan'
  | 'watchlist_brief'
  | 'multi_symbol_analysis'
  | 'report_compilation';

export type BackgroundJobStatus =
  | 'queued'
  | 'running'
  | 'completed'
  | 'failed'
  | 'cancelled';

export interface BackgroundJobSnapshot {
  jobId: string;
  workspaceId: string;
  kind: BackgroundJobKind;
  status: BackgroundJobStatus;
  progress?: number;
  label?: string;
  result?: Record<string, unknown>;
  error?: string;
  createdAt: string;
  updatedAt: string;
}

export interface StartBackgroundJobRequest {
  workspaceId: string;
  kind: BackgroundJobKind;
  params?: Record<string, unknown>;
}

export interface CancelBackgroundJobRequest {
  jobId: string;
}

export interface BackgroundJobEvent {
  jobId: string;
  workspaceId: string;
  kind: BackgroundJobKind;
  status: BackgroundJobStatus;
  progress?: number;
  label?: string;
  result?: Record<string, unknown>;
  error?: string;
}
