import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { SessionManager } from './manager.js';
import { SessionStore } from './store.js';
import { unlinkSync, existsSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';

describe('SessionManager', () => {
  const dbPath = join(tmpdir(), 'flynn-test-manager.db');
  let store: SessionStore;
  let manager: SessionManager;

  beforeEach(() => {
    store = new SessionStore(dbPath);
    manager = new SessionManager(store);
  });

  afterEach(() => {
    store.close();
    if (existsSync(dbPath)) {
      unlinkSync(dbPath);
    }
  });

  it('creates sessions for different frontends', () => {
    const telegramSession = manager.getSession('telegram', 'user-123');
    const tuiSession = manager.getSession('tui', 'local');

    expect(telegramSession.id).toBe('telegram:user-123');
    expect(tuiSession.id).toBe('tui:local');
  });

  it('returns same session for same frontend and user', () => {
    const session1 = manager.getSession('telegram', 'user-123');
    const session2 = manager.getSession('telegram', 'user-123');

    expect(session1).toBe(session2);
  });

  it('transfers session history between frontends', () => {
    const telegramSession = manager.getSession('telegram', 'user-123');
    telegramSession.addMessage({ role: 'user', content: 'Hello from Telegram' });
    telegramSession.addMessage({ role: 'assistant', content: 'Hi!' });

    const tuiSession = manager.getSession('tui', 'local');
    manager.transferSession('telegram', 'user-123', 'tui', 'local');

    const tuiMessages = tuiSession.getHistory();
    expect(tuiMessages).toHaveLength(2);
    expect(tuiMessages[0].content).toBe('Hello from Telegram');
  });

  it('lists active sessions', () => {
    manager.getSession('telegram', 'user-123');
    manager.getSession('tui', 'local');

    const sessions = manager.listSessions();
    expect(sessions).toContain('telegram:user-123');
    expect(sessions).toContain('tui:local');
  });

  it('indexes and searches history when enabled', () => {
    manager = new SessionManager(store, {
      enabled: true,
      maxKeywords: 8,
      searchLimit: 10,
      minScore: 0.1,
    });

    const session = manager.getSession('telegram', 'user-123');
    session.addMessage({ role: 'user', content: 'deploy backend api' });

    const results = manager.searchHistory('deploy backend');
    expect(results.length).toBeGreaterThan(0);
    expect(results[0].sessionId).toBe('telegram:user-123');
  });

  it('reindexHistory is safe and idempotent', () => {
    manager = new SessionManager(store, {
      enabled: true,
      maxKeywords: 8,
      searchLimit: 10,
      minScore: 0.1,
    });

    const session = manager.getSession('telegram', 'user-abc');
    session.addMessage({ role: 'user', content: 'history indexing test' });

    const first = manager.reindexHistory();
    const second = manager.reindexHistory();
    expect(first).toBeGreaterThan(0);
    expect(second).toBe(first);
  });
});
