import { Transform } from 'class-transformer';

/** Trims text; an empty optional field is stored as null. */
export const TrimOrNull = () =>
  Transform(({ value }) => {
    if (typeof value !== 'string') return value;
    const v = value.trim();
    return v === '' ? null : v;
  });

/** Trims text but keeps '' so required-field validation can reject it. */
export const Trim = () => Transform(({ value }) => (typeof value === 'string' ? value.trim() : value));

/** Lower-cases and trims email addresses. */
export const NormalizeEmail = () => Transform(({ value }) => (typeof value === 'string' ? value.trim().toLowerCase() : value));

export const PHONE_PATTERN = /^\+?[0-9][0-9\s()-]{5,19}$/;
export const PHONE_MESSAGE = 'Enter a valid phone number (digits, spaces, +, - or brackets)';

export const WEBSITE_PATTERN = /^(https?:\/\/)?([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(\/[^\s]*)?$/i;
export const WEBSITE_MESSAGE = 'Enter a valid website URL (e.g. https://example.com or example.com)';
