import type { ComponentProps } from "react";
import { Field, FieldError, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

type HiddenLabelInputProps = {
  name: string;
  label: string;
  /** Error message for this field: the field turns red and the message shows under it. */
  error?: string;
} & ComponentProps<"input">;

/** Text input whose label is screen-reader only — the design shows the label as a placeholder. */
export function HiddenLabelInput({ name, label, error, ...props }: HiddenLabelInputProps) {
  const id = `contact-${name}`;
  const errorId = `${id}-error`;
  return (
    <Field data-invalid={error ? true : undefined}>
      <FieldLabel htmlFor={id} className="sr-only">
        {label}
      </FieldLabel>
      {/* Phone (Figma 390): 12px text → 41px-tall fields. */}
      <Input
        id={id}
        name={name}
        placeholder={label}
        aria-invalid={error ? true : undefined}
        aria-describedby={error ? errorId : undefined}
        {...props}
        className="max-md:text-xs"
      />
      {error && (
        <FieldError id={errorId} className="text-xs leading-4">
          {error}
        </FieldError>
      )}
    </Field>
  );
}
