import { describe, expect, it } from 'bun:test';
import { buildChatHistoryEntry } from './useChatHistory';
import { TerminalEntry } from '../types/terminal';

const makeEntry = (overrides: Partial<TerminalEntry> & Pick<TerminalEntry, 'type' | 'content'>): TerminalEntry => ({
  id: `entry-${Math.random().toString(36).slice(2, 8)}`,
  timestamp: new Date(),
  ...overrides,
});

describe('buildChatHistoryEntry', () => {
  it('creates title, preview, command count, and strips empty trailing entries', () => {
    const prompt = 'What is the price of AAPL?';
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: prompt }),
      makeEntry({ type: 'response', content: 'The current price of AAPL is $150.00.' }),
      makeEntry({ type: 'thinking', content: '' }),
      makeEntry({ type: 'response', content: '' }),
    ];

    const entry = buildChatHistoryEntry(prompt, transcript);
    expect(entry).not.toBeNull();
    expect(entry!.title).toBe('What is the price of AAPL?');
    expect(entry!.responsePreview).toBe('The current price of AAPL is $150.00.');
    expect(entry!.commandCount).toBe(0);
    expect(entry!.transcript.length).toBe(2);
  });

  it('counts slash commands in commandCount', () => {
    const prompt = 'Search for NVDA';
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: prompt }),
      makeEntry({ type: 'command', content: '/search NVDA' }),
      makeEntry({ type: 'response', content: 'NVDA Corp.' }),
    ];

    const entry = buildChatHistoryEntry(prompt, transcript);
    expect(entry).not.toBeNull();
    expect(entry!.commandCount).toBe(1);
  });

  it('returns null for transcript with prompt only', () => {
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: 'Hello there' }),
    ];

    const entry = buildChatHistoryEntry('Hello there', transcript);
    expect(entry).toBeNull();
  });

  it('saves transcript with prompt plus response', () => {
    const prompt = 'Tell me about TSLA';
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: prompt }),
      makeEntry({ type: 'response', content: 'Tesla is an EV company.' }),
    ];

    const entry = buildChatHistoryEntry(prompt, transcript);
    expect(entry).not.toBeNull();
    expect(entry!.title).toBe('Tell me about TSLA');
  });

  it('saves transcript with prompt plus panel result', () => {
    const prompt = 'Show me AAPL financials';
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: prompt }),
      makeEntry({
        type: 'panel',
        content: { type: 'financials', data: {} as never },
      }),
    ];

    const entry = buildChatHistoryEntry(prompt, transcript);
    expect(entry).not.toBeNull();
  });

  it('saves transcript with prompt plus tool command plus panel result', () => {
    const prompt = 'Analyze MSFT';
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: prompt }),
      makeEntry({ type: 'command', content: '/fa MSFT' }),
      makeEntry({
        type: 'panel',
        content: { type: 'financials', data: {} as never },
      }),
    ];

    const entry = buildChatHistoryEntry(prompt, transcript);
    expect(entry).not.toBeNull();
    expect(entry!.commandCount).toBe(1);
  });

  it('truncates title to 48 characters', () => {
    const longPrompt = 'A'.repeat(100);
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: longPrompt }),
      makeEntry({ type: 'response', content: 'result' }),
    ];

    const entry = buildChatHistoryEntry(longPrompt, transcript);
    expect(entry!.title.length).toBe(48);
  });

  it('truncates response preview to 96 characters', () => {
    const longResponse = 'B'.repeat(200);
    const transcript: TerminalEntry[] = [
      makeEntry({ type: 'command', content: 'test' }),
      makeEntry({ type: 'response', content: longResponse }),
    ];

    const entry = buildChatHistoryEntry('test', transcript);
    expect(entry!.responsePreview!.length).toBe(96);
  });
});
