/**
 * i18n module — unit tests.
 */

import { describe, it, expect } from 'vitest';
import { UpsertTranslationSchema, BulkImportSchema, CreateLocaleSchema } from './types.js';

// ── UpsertTranslationSchema ──────────────────────────────────

describe('UpsertTranslationSchema', () => {
  it('accepts valid value', () => {
    const result = UpsertTranslationSchema.parse({ value: 'Save' });
    expect(result.value).toBe('Save');
  });

  it('rejects empty value', () => {
    expect(() => UpsertTranslationSchema.parse({ value: '' })).toThrow();
  });

  it('rejects value over 5000 chars', () => {
    expect(() => UpsertTranslationSchema.parse({ value: 'x'.repeat(5001) })).toThrow();
  });
});

// ── BulkImportSchema ─────────────────────────────────────────

describe('BulkImportSchema', () => {
  it('accepts valid translations map', () => {
    const result = BulkImportSchema.parse({
      translations: { 'common.save': 'Save', 'common.cancel': 'Cancel' },
    });
    expect(Object.keys(result.translations)).toHaveLength(2);
    expect(result.overwrite).toBe(false);
  });

  it('accepts overwrite flag', () => {
    const result = BulkImportSchema.parse({
      translations: { 'common.save': 'Sauvegarder' },
      overwrite: true,
    });
    expect(result.overwrite).toBe(true);
  });

  it('rejects empty value in translations', () => {
    expect(() => BulkImportSchema.parse({ translations: { 'common.save': '' } })).toThrow();
  });

  it('rejects value over 5000 chars in translations', () => {
    expect(() =>
      BulkImportSchema.parse({ translations: { 'common.save': 'x'.repeat(5001) } })
    ).toThrow();
  });
});

// ── CreateLocaleSchema ───────────────────────────────────────

describe('CreateLocaleSchema', () => {
  it('accepts valid locale', () => {
    const result = CreateLocaleSchema.parse({
      locale: 'fr',
      name: 'French',
      nativeName: 'Français',
    });
    expect(result.locale).toBe('fr');
    expect(result.direction).toBe('ltr');
    expect(result.enabled).toBe(true);
  });

  it('accepts locale with region', () => {
    const result = CreateLocaleSchema.parse({
      locale: 'fr-CA',
      name: 'French (Canada)',
      nativeName: 'Français (Canada)',
    });
    expect(result.locale).toBe('fr-CA');
  });

  it('accepts RTL direction', () => {
    const result = CreateLocaleSchema.parse({
      locale: 'ar',
      name: 'Arabic',
      nativeName: 'العربية',
      direction: 'rtl',
    });
    expect(result.direction).toBe('rtl');
  });

  it('rejects invalid locale format', () => {
    expect(() =>
      CreateLocaleSchema.parse({ locale: 'INVALID', name: 'Test', nativeName: 'Test' })
    ).toThrow();
  });

  it('rejects single-char locale', () => {
    expect(() =>
      CreateLocaleSchema.parse({ locale: 'f', name: 'Test', nativeName: 'Test' })
    ).toThrow();
  });

  it('rejects empty name', () => {
    expect(() =>
      CreateLocaleSchema.parse({ locale: 'fr', name: '', nativeName: 'Français' })
    ).toThrow();
  });

  it('rejects empty nativeName', () => {
    expect(() =>
      CreateLocaleSchema.parse({ locale: 'fr', name: 'French', nativeName: '' })
    ).toThrow();
  });

  it('defaults enabled to true', () => {
    const result = CreateLocaleSchema.parse({
      locale: 'de',
      name: 'German',
      nativeName: 'Deutsch',
    });
    expect(result.enabled).toBe(true);
  });

  it('accepts disabled locale', () => {
    const result = CreateLocaleSchema.parse({
      locale: 'de',
      name: 'German',
      nativeName: 'Deutsch',
      enabled: false,
    });
    expect(result.enabled).toBe(false);
  });
});
