import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { emptyInterest, validateField, validateInterest, type InterestValues } from './validation.ts';

const valid: InterestValues = {
  fullName: 'Layla Hassan',
  phone: '+20 100 123 4567',
  email: '',
  consent: true,
};

describe('validateInterest', () => {
  it('accepts a complete submission with the optional email left empty', () => {
    assert.deepEqual(validateInterest(valid), {});
  });

  it('flags every required field on an empty form', () => {
    assert.deepEqual(validateInterest(emptyInterest), {
      fullName: 'nameRequired',
      phone: 'phoneRequired',
      consent: 'consentRequired',
    });
  });

  it('treats whitespace-only input as empty', () => {
    assert.equal(validateField('fullName', { ...valid, fullName: '   ' }), 'nameRequired');
  });

  it('rejects one-character names', () => {
    assert.equal(validateField('fullName', { ...valid, fullName: 'A' }), 'nameShort');
  });
});

describe('phone', () => {
  const check = (phone: string) => validateField('phone', { ...valid, phone });

  it('accepts local and international formats', () => {
    for (const phone of ['01001234567', '+20 100 123 4567', '(02) 1234-5678']) assert.equal(check(phone), undefined, phone);
  });

  it('rejects letters, too few and too many digits', () => {
    for (const phone of ['12ab', '1234567', '1234567890123456']) assert.equal(check(phone), 'phoneInvalid', phone);
  });
});

describe('email', () => {
  const check = (email: string) => validateField('email', { ...valid, email });

  it('is optional', () => assert.equal(check(''), undefined));
  it('accepts a normal address', () => assert.equal(check('layla@example.com'), undefined));
  it('rejects malformed addresses', () => {
    for (const email of ['not-an-email', 'a@b', '@example.com']) assert.equal(check(email), 'emailInvalid', email);
  });
});
