"use client";

/**
 * A single PO Terms & Conditions slot (issue #11) — a native
 * <select name={field}> sourced from the admin-managed clauses for its category.
 * Plain HTML so it works with the forms' native FormData submission.
 *
 * `options` are the active clause texts (also the stored value). `current` is the
 * PO's existing/default value for this slot; if it isn't one of the active
 * options (a removed clause, or an older custom value) it is preserved as a
 * leading "(current)" option so an edit never silently drops it.
 */
export function TermsField({
  field,
  options,
  current,
  className,
}: {
  field: string;
  options: string[];
  current?: string | null;
  className?: string;
}) {
  const cur = (current ?? "").trim();
  const currentMissing = cur.length > 0 && !options.includes(cur);

  return (
    <select name={field} defaultValue={cur} className={className}>
      <option value="">—</option>
      {currentMissing && <option value={cur}>{cur} (current)</option>}
      {options.map((o) => (
        <option key={o} value={o}>
          {o}
        </option>
      ))}
    </select>
  );
}
