import { describe, expect, it } from 'vitest';
import {
  getHermesAgents,
  getHermesHistory,
  getHermesOverview,
  getHermesProductById,
  getHermesProducts,
  getHermesSettings,
  getHermesTaskById,
  getHermesTaskEvents,
  getHermesTasks,
  hermesProducts,
  hermesTasks,
} from './hermes.js';

describe('hermes mock service', () => {
  it('exposes a large product portfolio', () => {
    expect(hermesProducts.length).toBeGreaterThanOrEqual(50);
    expect(getHermesProducts('needs-attention').length).toBeGreaterThan(0);
  });

  it('filters tasks by query and status', () => {
    const blocked = getHermesTasks({ status: 'blocked' });
    expect(blocked.every((task) => task.status === 'blocked')).toBe(true);

    const queried = getHermesTasks({ query: 'deployment' });
    expect(queried.length).toBeGreaterThan(0);
    expect(queried.some((task) => task.title.toLowerCase().includes('deployment') || task.description.toLowerCase().includes('deployment'))).toBe(true);
  });

  it('returns task details and timeline events', () => {
    const task = getHermesTaskById(hermesTasks[0].id);
    expect(task).toBeDefined();
    expect(getHermesTaskEvents(task!.id).length).toBeGreaterThan(0);
  });

  it('computes overview metrics', () => {
    const overview = getHermesOverview();
    expect(overview.completedToday).toBeGreaterThanOrEqual(0);
    expect(overview.successRate).toBeGreaterThanOrEqual(0);
    expect(overview.lastAction.length).toBeGreaterThan(0);
  });

  it('returns other mock observability slices', () => {
    expect(getHermesAgents().length).toBeGreaterThan(0);
    expect(getHermesHistory().length).toBeGreaterThan(0);
    expect(getHermesSettings().registry.length).toBeGreaterThan(0);
    expect(getHermesProductById(hermesProducts[0].id)).toBeDefined();
  });

  it('tags every product/task with a Hermes instance', () => {
    expect(hermesProducts.every((p) => p.instanceId === 'vijay' || p.instanceId === 'bheem')).toBe(true);
    expect(hermesTasks.every((t) => t.instanceId === 'vijay' || t.instanceId === 'bheem')).toBe(true);
    // Both instances should be represented in the seed data so the switcher
    // doesn't bottom out on either one.
    expect(hermesProducts.some((p) => p.instanceId === 'vijay')).toBe(true);
    expect(hermesProducts.some((p) => p.instanceId === 'bheem')).toBe(true);
  });

  it('filters tasks/products/agents/overview by instance', () => {
    const vijayTasks = getHermesTasks({ instance: 'vijay' });
    const bheemTasks = getHermesTasks({ instance: 'bheem' });
    expect(vijayTasks.every((t) => t.instanceId === 'vijay')).toBe(true);
    expect(bheemTasks.every((t) => t.instanceId === 'bheem')).toBe(true);
    expect(vijayTasks.length + bheemTasks.length).toBe(hermesTasks.length);

    const vijayProducts = getHermesProducts('all', 'vijay');
    expect(vijayProducts.every((p) => p.instanceId === 'vijay')).toBe(true);

    // Cross-cutting agents (scope `'all'`) appear in every per-instance view;
    // strict per-instance agents only appear when their instance matches.
    const vijayAgents = getHermesAgents('vijay');
    const bheemAgents = getHermesAgents('bheem');
    expect(vijayAgents.every((a) => a.instanceId === 'vijay' || a.instanceId === 'all')).toBe(true);
    expect(bheemAgents.every((a) => a.instanceId === 'bheem' || a.instanceId === 'all')).toBe(true);
    expect(vijayAgents.some((a) => a.id === 'cli-runner')).toBe(true);
    expect(bheemAgents.some((a) => a.id === 'local-vm-runner')).toBe(true);
    expect(vijayAgents.some((a) => a.id === 'local-vm-runner')).toBe(false);

    const overviewAll = getHermesOverview('all');
    const overviewVijay = getHermesOverview('vijay');
    expect(overviewVijay.activeTasks).toBeLessThanOrEqual(overviewAll.activeTasks);
  });
});
