import { describe, expect, it } from 'vitest';
import { brandSlug, slugify, uniqueSlug } from '../src/shared/slug';
import { planBackfill, TABLES } from '../src/db/migrations/20261127000001_slug_backfill';

describe('slug generator', () => {
  it('makes readable, lowercase, hyphenated slugs', () => {
    expect(brandSlug('Napa Extend', '665 mg', 'Tablet')).toBe('napa-extend-665-mg-tablet');
    expect(brandSlug('Napa', '120 mg/5 ml', 'Syrup')).toBe('napa-120-mg-5-ml-syrup');
    expect(slugify('  Rivotril 0.5  ')).toBe('rivotril-0-5');
  });
  it('handles combinations and special characters', () => {
    expect(slugify('Amlodipine + Atenolol')).toBe('amlodipine-atenolol');
    expect(slugify('Paracetamol+Caffeine')).toBe('paracetamol-caffeine');
    expect(slugify('Vitamin B₁₂ 100 µg')).toBe('vitamin-b12-100-mcg');
    expect(slugify('Hydrocortisone 1% Cream')).toBe('hydrocortisone-1-percent-cream');
    expect(slugify('Café & Co.')).toBe('cafe-and-co');
    expect(slugify('নাপা')).toBe(''); // Bangla-only names need an English name for the slug
  });
  it('adds a number only when the slug is taken', () => {
    const used = new Set(['napa-500-mg-tablet', 'napa-500-mg-tablet-2']);
    expect(uniqueSlug('napa-500-mg-tablet', (s) => used.has(s))).toBe('napa-500-mg-tablet-3');
    expect(uniqueSlug('ace-500-mg-tablet', (s) => used.has(s))).toBe('ace-500-mg-tablet');
  });
});

describe('slug backfill migration', () => {
  const brands = TABLES.find((t) => t.table === 'brands')!;
  const tags = TABLES.find((t) => t.table === 'catalogue_tags')!;

  it('leaves valid slugs alone and fixes empty or invalid ones, with redirects for old slugs', () => {
    const fixes = planBackfill(brands, [
      {
        id: '1',
        slug: 'napa-500-mg-tablet',
        name: 'Napa',
        strength: '500 mg',
        form_label: 'Tablet',
        catalogue: 'human',
      },
      { id: '2', slug: '', name: 'Napa', strength: '500 mg', form_label: 'Tablet', catalogue: 'human' },
      { id: '3', slug: 'Ace 500', name: 'Ace', strength: '500 mg', form_label: 'Tablet', catalogue: 'human' },
      {
        id: '4',
        slug: 'Renamycin Vet',
        name: 'Renamycin',
        strength: '10%',
        form_label: 'Injection',
        catalogue: 'vet',
      },
    ]);
    expect(fixes).toEqual([
      { id: '2', old: '', slug: 'napa-500-mg-tablet-2', entity: 'brand' },
      { id: '3', old: 'Ace 500', slug: 'ace-500-mg-tablet', entity: 'brand' },
      { id: '4', old: 'Renamycin Vet', slug: 'renamycin-10-percent-injection', entity: 'product' },
    ]);
  });

  it('keeps catalogue tag slugs unique per catalogue and kind only', () => {
    const fixes = planBackfill(tags, [
      { id: '1', slug: 'rice', name: 'Rice', catalogue: 'agro', kind: 'crop' },
      { id: '2', slug: '', name: 'Rice', catalogue: 'agro', kind: 'product_type' },
      { id: '3', slug: '', name: 'Rice', catalogue: 'agro', kind: 'crop' },
    ]);
    expect(fixes.map((f) => f.slug)).toEqual(['rice', 'rice-2']);
    expect(fixes.every((f) => f.entity === null)).toBe(true);
  });
});
