import { CommandSuggestion, PortfolioAction } from '../types/terminal';

export interface TerminalCommandArgumentSpec {
  key: string;
  label: string;
  description: string;
  placeholder: string;
  required: boolean;
  options?: string[];
  consumeRest?: boolean;
}

export interface TerminalCommandSpec extends CommandSuggestion {
  arguments: TerminalCommandArgumentSpec[];
  examples?: string[];
  portfolioAction?: Extract<
    PortfolioAction,
    'buy' | 'sell' | 'deposit' | 'withdraw'
  >;
}

export const TERMINAL_COMMAND_SPECS: TerminalCommandSpec[] = [
  {
    command: '/search',
    description: 'Search live security data',
    category: 'search',
    arguments: [
      {
        key: 'query',
        label: 'Query',
        description: 'Ticker or company name to search for.',
        placeholder: 'ticker-or-company',
        required: true,
        consumeRest: true,
      },
    ],
    examples: ['/search AAPL', '/search Apple'],
  },
  {
    command: '/portfolio',
    description: 'Show portfolio overview',
    category: 'portfolio',
    arguments: [],
    examples: ['/portfolio'],
  },
  {
    command: '/portfolio stats',
    description: 'Show portfolio statistics',
    category: 'portfolio',
    arguments: [],
    examples: ['/portfolio stats'],
  },
  {
    command: '/portfolio history',
    description: 'Show recent portfolio transactions',
    category: 'portfolio',
    arguments: [
      {
        key: 'limit',
        label: 'Limit',
        description: 'Optional number of transactions to return.',
        placeholder: 'limit',
        required: false,
      },
    ],
    examples: ['/portfolio history', '/portfolio history 25'],
  },
  {
    command: '/buy',
    description: 'Create a buy order',
    category: 'portfolio',
    portfolioAction: 'buy',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to purchase.',
        placeholder: 'ticker',
        required: true,
      },
      {
        key: 'quantity',
        label: 'Quantity',
        description: 'Number of shares to buy.',
        placeholder: 'quantity',
        required: true,
      },
      {
        key: 'price',
        label: 'Price',
        description: 'Optional execution price override.',
        placeholder: 'price',
        required: false,
      },
    ],
    examples: ['/buy NVDA 10', '/buy NVDA 10 875.50'],
  },
  {
    command: '/sell',
    description: 'Create a sell order',
    category: 'portfolio',
    portfolioAction: 'sell',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to sell.',
        placeholder: 'ticker',
        required: true,
      },
      {
        key: 'quantity',
        label: 'Quantity',
        description: 'Number of shares to sell.',
        placeholder: 'quantity',
        required: true,
      },
      {
        key: 'price',
        label: 'Price',
        description: 'Optional execution price override.',
        placeholder: 'price',
        required: false,
      },
    ],
    examples: ['/sell MSFT 5', '/sell MSFT 5 440'],
  },
  {
    command: '/cash deposit',
    description: 'Add cash to the portfolio',
    category: 'cash',
    portfolioAction: 'deposit',
    arguments: [
      {
        key: 'amount',
        label: 'Amount',
        description: 'Cash amount to deposit.',
        placeholder: 'amount',
        required: true,
      },
    ],
    examples: ['/cash deposit 1000'],
  },
  {
    command: '/cash withdraw',
    description: 'Withdraw cash from the portfolio',
    category: 'cash',
    portfolioAction: 'withdraw',
    arguments: [
      {
        key: 'amount',
        label: 'Amount',
        description: 'Cash amount to withdraw.',
        placeholder: 'amount',
        required: true,
      },
    ],
    examples: ['/cash withdraw 500'],
  },
  {
    command: '/fa',
    description: 'SEC financial statements',
    category: 'financials',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to load filings for.',
        placeholder: 'ticker',
        required: true,
      },
      {
        key: 'period',
        label: 'Period',
        description: 'Reporting cadence.',
        placeholder: 'annual-or-quarterly',
        required: false,
        options: ['annual', 'quarterly'],
      },
    ],
    examples: ['/fa AAPL', '/fa AAPL quarterly'],
  },
  {
    command: '/cf',
    description: 'SEC cash flow summary',
    category: 'cashflow',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to load filings for.',
        placeholder: 'ticker',
        required: true,
      },
      {
        key: 'period',
        label: 'Period',
        description: 'Reporting cadence.',
        placeholder: 'annual-or-quarterly',
        required: false,
        options: ['annual', 'quarterly'],
      },
    ],
    examples: ['/cf KO', '/cf KO annual'],
  },
  {
    command: '/dvd',
    description: 'SEC dividends history',
    category: 'dividends',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to load dividends for.',
        placeholder: 'ticker',
        required: true,
      },
    ],
    examples: ['/dvd KO'],
  },
  {
    command: '/em',
    description: 'SEC earnings history',
    category: 'earnings',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to load earnings for.',
        placeholder: 'ticker',
        required: true,
      },
      {
        key: 'period',
        label: 'Period',
        description: 'Reporting cadence.',
        placeholder: 'annual-or-quarterly',
        required: false,
        options: ['annual', 'quarterly'],
      },
    ],
    examples: ['/em SAP', '/em SAP quarterly'],
  },
  {
    command: '/news',
    description: 'Market news',
    category: 'news',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Optional symbol to filter news.',
        placeholder: 'ticker',
        required: false,
      },
    ],
    examples: ['/news', '/news NVDA'],
  },
  {
    command: '/analyze',
    description: 'AI analysis',
    category: 'analysis',
    arguments: [
      {
        key: 'ticker',
        label: 'Ticker',
        description: 'Security symbol to analyze.',
        placeholder: 'ticker',
        required: true,
      },
    ],
    examples: ['/analyze AAPL'],
  },
  {
    command: '/notes add',
    description: 'Open quick research note capture',
    category: 'research',
    arguments: [],
    examples: ['/notes add'],
  },
  {
    command: '/notes current',
    description: 'Capture the current terminal context as a research note',
    category: 'research',
    arguments: [],
    examples: ['/notes current'],
  },
  {
    command: '/help',
    description: 'List commands',
    category: 'system',
    arguments: [],
    examples: ['/help'],
  },
  {
    command: '/clear',
    description: 'Clear the current terminal workspace',
    category: 'system',
    arguments: [],
    examples: ['/clear'],
  },
];

export const TERMINAL_COMMAND_SUGGESTIONS: CommandSuggestion[] =
  TERMINAL_COMMAND_SPECS.map(({ command, description, category }) => ({
    command,
    description,
    category,
  }));

export const getTerminalCommandSpec = (
  command: string,
): TerminalCommandSpec | undefined =>
  TERMINAL_COMMAND_SPECS.find((spec) => spec.command === command);

export const getTerminalCommandSpecForAction = (
  action: Extract<PortfolioAction, 'buy' | 'sell' | 'deposit' | 'withdraw'>,
): TerminalCommandSpec | undefined =>
  TERMINAL_COMMAND_SPECS.find((spec) => spec.portfolioAction === action);
