/**
 * i18n PR4 - subdivisions.
 *
 * Validates:
 *  1. Major countries (PL, US, CA, GB, AU) return non-empty lists
 *  2. Sample known codes resolve to expected names
 *  3. hasSubdivisions correctly classifies micro-states with no subs
 *  4. Validation rejects garbage codes
 *  5. getSubdivisionName returns the human name
 */

import { describe, it, expect } from 'vitest';
import {
  subdivisionsForCountry,
  hasSubdivisions,
  isValidSubdivisionCode,
  getSubdivisionName,
} from '@/lib/i18n/subdivisions';

describe('i18n subdivisions', () => {
  it('major countries return populated subdivision lists', () => {
    expect(subdivisionsForCountry('PL').length).toBeGreaterThanOrEqual(16);
    expect(subdivisionsForCountry('US').length).toBeGreaterThanOrEqual(50);
    expect(subdivisionsForCountry('CA').length).toBeGreaterThanOrEqual(13);
    expect(subdivisionsForCountry('GB').length).toBeGreaterThan(4); // GB has constituent + 200+ admin areas
    expect(subdivisionsForCountry('AU').length).toBeGreaterThanOrEqual(8);
  });

  it('returns sorted-by-name results', () => {
    const us = subdivisionsForCountry('US');
    const names = us.map((s) => s.name);
    const sortedCopy = [...names].sort((a, b) => a.localeCompare(b));
    expect(names).toEqual(sortedCopy);
  });

  it('hasSubdivisions is true for known countries', () => {
    expect(hasSubdivisions('US')).toBe(true);
    expect(hasSubdivisions('PL')).toBe(true);
    expect(hasSubdivisions('GB')).toBe(true);
  });

  it('isValidSubdivisionCode accepts known codes and rejects junk', () => {
    expect(isValidSubdivisionCode('US-CA')).toBe(true);
    expect(isValidSubdivisionCode('PL-MZ')).toBe(true);
    expect(isValidSubdivisionCode('US-XX')).toBe(false);
    expect(isValidSubdivisionCode('GARBAGE')).toBe(false);
    expect(isValidSubdivisionCode('')).toBe(false);
  });

  it('getSubdivisionName returns the human name', () => {
    expect(getSubdivisionName('US-CA')).toMatch(/California/);
    expect(getSubdivisionName('PL-MZ')).toMatch(/Mazowieckie|Masovian/);
    expect(getSubdivisionName('AU-NSW')).toMatch(/New South Wales/);
    // Unknown -> code itself
    expect(getSubdivisionName('XX-YY')).toBe('XX-YY');
  });
});
