import { describe, expect, it } from 'bun:test';
import {
  resolveTerminalCaptureWorkspaceId,
  resolveTerminalResearchCaptureIntent,
} from './terminalResearchCapture';
import type { TerminalEntry } from '../types/terminal';
import type { ResearchWorkspace } from '../types/research';

describe('resolveTerminalResearchCaptureIntent', () => {
  const history: TerminalEntry[] = [
    {
      id: 'command-1',
      type: 'command',
      content: '/search NVDA',
      timestamp: new Date('2026-04-09T10:00:00Z'),
    },
    {
      id: 'panel-1',
      type: 'panel',
      content: {
        type: 'company',
        data: {
          symbol: 'NVDA',
          name: 'NVIDIA',
          price: 900,
          change: 12,
          changePercent: 1.35,
          marketCap: 2_000_000_000_000,
          profile: {
            sector: 'Semiconductors',
            description: 'GPU leader.',
          },
        },
      },
      timestamp: new Date('2026-04-09T10:00:01Z'),
    },
  ];

  it('creates a direct-open research intent for quick notes', () => {
    const intent = resolveTerminalResearchCaptureIntent('/notes add', history);

    expect(intent).toEqual({
      ticker: 'NVDA',
      terminalNoteSeed: {
        rawText: '',
        ticker: 'NVDA',
        contextLabel: 'NVDA company snapshot',
      },
    });
  });

  it('creates a seeded research intent for current terminal context', () => {
    const intent = resolveTerminalResearchCaptureIntent('/notes current', history);

    expect(intent?.ticker).toBe('NVDA');
    expect(intent?.terminalNoteSeed?.ticker).toBe('NVDA');
    expect(intent?.terminalNoteSeed?.contextLabel).toBe('NVDA company context');
    expect(intent?.terminalNoteSeed?.rawText).toContain(
      'Current terminal context: company snapshot for NVDA',
    );
    expect(intent?.terminalNoteSeed?.rawText).toContain('Analyst note:');
  });

  it('prefers a workspace whose primary ticker matches the terminal capture', () => {
    const workspaces: ResearchWorkspace[] = [
      {
        id: 'workspace-aapl',
        name: 'AAPL Research',
        primaryTicker: 'AAPL',
        scope: 'single_company',
        stage: 'capture',
        defaultView: 'canvas',
        pinnedNoteIds: [],
        archived: false,
        createdAt: '2026-04-09T10:00:00Z',
        updatedAt: '2026-04-09T10:00:00Z',
      },
      {
        id: 'workspace-nvda',
        name: 'NVDA Research',
        primaryTicker: 'NVDA',
        scope: 'single_company',
        stage: 'capture',
        defaultView: 'canvas',
        pinnedNoteIds: [],
        archived: false,
        createdAt: '2026-04-09T10:00:00Z',
        updatedAt: '2026-04-09T10:00:00Z',
      },
    ];

    expect(
      resolveTerminalCaptureWorkspaceId({
        ticker: 'nvda',
        workspaces,
        activeWorkspaceId: 'workspace-aapl',
      }),
    ).toBe('workspace-nvda');
  });

  it('falls back to the active workspace when the terminal capture has no ticker', () => {
    const workspaces: ResearchWorkspace[] = [
      {
        id: 'workspace-aapl',
        name: 'AAPL Research',
        primaryTicker: 'AAPL',
        scope: 'single_company',
        stage: 'capture',
        defaultView: 'canvas',
        pinnedNoteIds: [],
        archived: false,
        createdAt: '2026-04-09T10:00:00Z',
        updatedAt: '2026-04-09T10:00:00Z',
      },
    ];

    expect(
      resolveTerminalCaptureWorkspaceId({
        workspaces,
        activeWorkspaceId: 'workspace-aapl',
      }),
    ).toBe('workspace-aapl');
  });
});
