/**
 * Make a value match LITERALLY in a PostgreSQL LIKE / ILIKE pattern.
 *
 * `_` and `%` are wildcards in LIKE. Email addresses routinely contain `_`
 * (every test login here does), so `ilike('email', user.email)` was a
 * pattern, not an equality: `test_d_iver@users.local` matched the trips of
 * `test_driver@users.local`. Verified against the live database on
 * 2026-09-11. Wherever an email is used to decide who a row belongs to —
 * trip visibility, linking invitations at sign-in, auto-attaching invitees —
 * it must go through this.
 *
 * Backslash is PostgreSQL's default LIKE escape, and PostgREST passes it
 * through unchanged both in `.ilike()` and inside `.or()` filters.
 */
export function escapeLike(value: string): string {
  return value.replace(/[\\%_]/g, (c) => `\\${c}`)
}

/** Case-insensitive EXACT match on an email column: `.ilike(col, emailPattern(e))`. */
export function emailPattern(email: string): string {
  return escapeLike(email.trim())
}
