import {
  GROUP_ENTRY_STATUS,
  PIPELINE_GROUPS,
  PIPELINE_STATUSES,
  groupOf,
  isProspect,
  normalizeStatus,
  statusLabel,
  statusTone,
} from './pipeline';

describe('pipeline', () => {
  test('normalizeStatus canonicalizes casing and synonyms', () => {
    expect(normalizeStatus('applied')).toBe('Applied');
    expect(normalizeStatus('  OFFER ')).toBe('Offer');
    expect(normalizeStatus('Interviewing')).toBe('Interview');
    expect(normalizeStatus('declined')).toBe('Rejected');
  });

  test('normalizeStatus preserves unknown as Other and empty as Applied', () => {
    expect(normalizeStatus('Take-home')).toBe('Other');
    expect(normalizeStatus('')).toBe('Applied');
    expect(normalizeStatus(null)).toBe('Applied');
  });

  test('statusTone maps stages to palette keys', () => {
    expect(statusTone('Offer')).toBe('success');
    expect(statusTone('Rejected')).toBe('error');
    expect(statusTone('Waiting')).toBe('warning');
    expect(statusTone('Ghosted')).toBe('warning');
    expect(statusTone('Interview')).toBe('info');
    expect(statusTone('Applied')).toBe('primary');
    expect(statusTone('Take-home')).toBe('default');
  });

  test('statusLabel localizes canonical and passes through custom', () => {
    const t = (key: string) => ({ statusApplied: 'Applied', statusOffer: 'Offer' } as Record<string, string>)[key] ?? key;
    expect(statusLabel(t, 'Applied')).toBe('Applied');
    expect(statusLabel(t, 'Interviewing')).toBe('statusInterview'); // maps to canonical key
    expect(statusLabel(t, 'Take-home assignment')).toBe('Take-home assignment');
  });

  test('canonical stage list is stable and ordered', () => {
    // Mirrors the backend JobPipeline.Stages. Waiting and Ghosted are retained deliberately:
    // Ghosted is where the rules engine parks an unanswered job, and Waiting has its own
    // follow-up rule.
    expect(PIPELINE_STATUSES).toEqual([
      'Saved',
      'Interested',
      'Preparing',
      'Applied',
      'Waiting',
      'Interview',
      'Offer',
      'Rejected',
      'Ghosted',
      'Withdrawn',
    ]);
  });

  test('normalizeStatus canonicalizes the new stage synonyms', () => {
    expect(normalizeStatus('bookmarked')).toBe('Saved');
    expect(normalizeStatus('shortlisted')).toBe('Interested');
    expect(normalizeStatus('drafting')).toBe('Preparing');
    expect(normalizeStatus('withdrew')).toBe('Withdrawn');
    expect(normalizeStatus('cancelled')).toBe('Withdrawn');
    // Opposite directions: the employer declined you vs you pulled out.
    expect(normalizeStatus('declined')).toBe('Rejected');
  });

  test('board groups match the agreed layout', () => {
    expect(PIPELINE_GROUPS.map((g) => g.key)).toEqual(['NotApplied', 'Active', 'Closed']);
    expect(PIPELINE_GROUPS[0].statuses).toEqual(['Saved', 'Interested', 'Preparing']);
    expect(PIPELINE_GROUPS[1].statuses).toEqual(['Applied', 'Waiting', 'Interview', 'Offer']);
    expect(PIPELINE_GROUPS[2].statuses).toEqual(['Rejected', 'Ghosted', 'Withdrawn']);
  });

  test('every stage belongs to exactly one group, and groups agree with the backend', () => {
    const grouped = PIPELINE_GROUPS.flatMap((g) => g.statuses);
    expect(grouped.slice().sort()).toEqual(PIPELINE_STATUSES.slice().sort());
    expect(new Set(grouped).size).toBe(grouped.length);
  });

  test('groupOf and isProspect classify stages', () => {
    expect(groupOf('Saved')).toBe('NotApplied');
    expect(groupOf('Offer')).toBe('Active');
    expect(groupOf('Withdrawn')).toBe('Closed');
    expect(groupOf('Take-home assignment')).toBeNull();

    expect(isProspect('Preparing')).toBe(true);
    expect(isProspect('Applied')).toBe(false);
    // Custom statuses predate the split and have always counted as applied.
    expect(isProspect('Take-home assignment')).toBe(false);
  });

  test('drag entry stages never infer Ghosted or Withdrawn', () => {
    // A coarse drag must not claim the rules engine's conclusion (Ghosted) or the user's own
    // action (Withdrawn).
    expect(GROUP_ENTRY_STATUS).toEqual({ NotApplied: 'Saved', Active: 'Applied', Closed: 'Rejected' });
  });

  test('statusTone covers every stage', () => {
    expect(statusTone('Saved')).toBe('default');
    expect(statusTone('Withdrawn')).toBe('error');
    for (const s of PIPELINE_STATUSES) {
      expect(typeof statusTone(s)).toBe('string');
    }
  });
});
