import { describe, it, expect } from 'vitest';
import { TeamPulseSyncEmitter } from '../event-emitter';
import { SyncEvent, TeamPulseConfig, DEFAULT_TEAM_PULSE_CONFIG } from '../types';

const enabledConfig: TeamPulseConfig = {
  ...DEFAULT_TEAM_PULSE_CONFIG,
  enabled: true,
  userId: 'leo',
};

describe('TeamPulseSyncEmitter', () => {
  it('emits task_selected events when enabled', () => {
    const emitter = new TeamPulseSyncEmitter(enabledConfig, 'workspace-1');
    let emitted: SyncEvent | null = null;
    emitter.on('sync_event', (event: SyncEvent) => {
      emitted = event;
    });

    emitter.emitTaskSelected({
      id: 'TASK-001',
      task: 'Fix auth bug',
      project: 'api',
      priority: 'P0',
      status: 'in_progress',
      startedAt: '2026-02-18T10:00:00Z',
    });

    expect(emitted).not.toBeNull();
    expect(emitted!.eventType).toBe('task_selected');
    expect(emitted!.data.taskId).toBe('TASK-001');
    expect(emitted!.userId).toBe('leo');
    expect(emitted!.visibility).toBe('team');
  });

  it('does not emit when disabled', () => {
    const disabledConfig = { ...enabledConfig, enabled: false };
    const emitter = new TeamPulseSyncEmitter(disabledConfig, 'workspace-1');
    let emitted = false;
    emitter.on('sync_event', () => {
      emitted = true;
    });

    emitter.emitTaskSelected({ id: 'TASK-001', task: 'Fix bug', project: 'api' });
    expect(emitted).toBe(false);
  });

  it('respects private project rules', () => {
    const privateConfig: TeamPulseConfig = {
      ...enabledConfig,
      privacyRules: {
        ...enabledConfig.privacyRules,
        privateProjects: ['secret'],
      },
    };
    const emitter = new TeamPulseSyncEmitter(privateConfig, 'workspace-1');
    let emitted: SyncEvent | null = null;
    emitter.on('sync_event', (event: SyncEvent) => {
      emitted = event;
    });

    emitter.emitTaskCreated({
      id: 'TASK-002',
      task: 'Hidden',
      project: 'secret',
      priority: 'P2',
      status: 'open',
    });
    expect(emitted).toBeNull();
  });
});
