import { describe, it, expect } from 'vitest';

import { parseSalesFilters } from '@/lib/services/reports/sales-filters';

function params(init: Record<string, string>): URLSearchParams {
  return new URLSearchParams(init);
}

describe('parseSalesFilters', () => {
  it('returns undefined when no filter params are present', () => {
    expect(parseSalesFilters(params({}))).toBeUndefined();
    // from/to/compare are NOT filters — they must not produce a filter object.
    expect(
      parseSalesFilters(params({ from: '2026-01-01', to: '2026-02-01', compare: '1' })),
    ).toBeUndefined();
  });

  it('parses the pre-existing stage / leadCategory / outcome CSV params', () => {
    const f = parseSalesFilters(
      params({
        stage: 'eoi,reservation',
        leadCategory: 'hot_lead',
        outcome: 'won,cancelled',
      }),
    );
    expect(f?.stages).toEqual(['eoi', 'reservation']);
    expect(f?.leadCategories).toEqual(['hot_lead']);
    expect(f?.outcomes).toEqual(['won', 'cancelled']);
  });

  it('parses the new source filter against the SOURCES allowlist', () => {
    const f = parseSalesFilters(params({ source: 'website,broker' }));
    expect(f?.sources).toEqual(['website', 'broker']);
  });

  it('drops unknown source values (stale bookmark degrades to no filter)', () => {
    // 'tiktok' is not a valid source — it gets stripped; only 'referral' survives.
    const f = parseSalesFilters(params({ source: 'tiktok,referral' }));
    expect(f?.sources).toEqual(['referral']);
  });

  it('treats an all-invalid source param as no source filter', () => {
    const f = parseSalesFilters(params({ source: 'tiktok,carrierpigeon' }));
    expect(f).toBeUndefined();
  });

  it('parses the new assignedTo (rep) filter as a free list of user ids', () => {
    // Rep ids are UUIDs — there is no static allowlist, so any non-empty
    // value passes through. Drizzle parameterises inArray, so this is safe.
    const f = parseSalesFilters(params({ assignedTo: 'usr_11111111,usr_22222222' }));
    expect(f?.assignedTo).toEqual(['usr_11111111', 'usr_22222222']);
  });

  it('trims whitespace and drops empty entries in assignedTo', () => {
    const f = parseSalesFilters(params({ assignedTo: ' usr_a , , usr_b ' }));
    expect(f?.assignedTo).toEqual(['usr_a', 'usr_b']);
  });

  it('combines source + assignedTo with the existing filters', () => {
    const f = parseSalesFilters(
      params({
        stage: 'eoi',
        source: 'website',
        assignedTo: 'usr_a',
      }),
    );
    expect(f).toEqual({
      stages: ['eoi'],
      leadCategories: undefined,
      outcomes: undefined,
      sources: ['website'],
      assignedTo: ['usr_a'],
    });
  });
});
