"use client";

import type { ReactNode } from "react";
import { useTranslation } from "@enode/core/translations/hooks";

// One labelled input as a standard settings row: the title on the left, the
// input element filling the right edge (an H-stack), matching Row/ToggleRow's
// px-4 py-3 so it lines up with the other rows in the same Card. Carries an
// optional required/"Optional" marker, a trailing label slot (e.g. an info
// tooltip), and a hint line under the row (validation error / side-effect note).
// Extracted from the user drawer so form drawers (users, live sessions) share
// one row vocabulary.
export function FieldRow({
  label,
  required = false,
  optional = false,
  labelTrailing,
  hint,
  children,
}: {
  label: string;
  required?: boolean;
  optional?: boolean;
  // Rendered at the end of the label (e.g. an info tooltip).
  labelTrailing?: ReactNode;
  // Helper/validation text rendered under the row.
  hint?: ReactNode;
  // The input element, placed on the trailing edge.
  children: ReactNode;
}) {
  const { t } = useTranslation();
  return (
    <div className="px-4 py-3">
      {/* A <label>: the field's input gets the row's title as its accessible
          name, and a click on the title focuses it. */}
      <label className="flex items-center gap-3">
        <span className="body-prominent flex shrink-0 items-center gap-1.5">
          {label}
          {required && (
            <>
              <span aria-hidden className="text-foreground-red-orange">
                *
              </span>
              <span className="sr-only">{t("(required)")}</span>
            </>
          )}
          {optional && (
            <span className="body-small text-foreground-more-muted">
              {t("Optional")}
            </span>
          )}
          {labelTrailing}
        </span>
        <div className="ml-auto flex items-center justify-end">
          {children}
        </div>
      </label>
      {hint != null && (
        <p className="body-small mt-1.5 text-foreground-muted">{hint}</p>
      )}
    </div>
  );
}
