import React from 'react';
import { cn } from '@/lib/utils';

interface AuthFormFieldProps {
  label: string;
  error?: string;
  required?: boolean;
  helpText?: string;
  children: React.ReactNode;
  className?: string;
  htmlFor?: string;
}

export function AuthFormField({
  label,
  error,
  required = false,
  helpText,
  children,
  className,
  htmlFor,
}: AuthFormFieldProps) {
  const fieldId =
    htmlFor || `auth-field-${Math.random().toString(36).substr(2, 9)}`;

  return (
    <div className={cn('w-full space-y-2', className)}>
      <label
        htmlFor={fieldId}
        className="block text-sm font-medium text-kodo-text-main dark:text-kodo-text-main"
      >
        {label}
        {required && (
          <span className="text-kodo-red ml-1" aria-label="required">
            *
          </span>
        )}
      </label>
      <div id={fieldId}>{children}</div>
      {helpText && !error && (
        <p className="text-xs text-kodo-content-dim dark:text-kodo-content-dim">{helpText}</p>
      )}
      {error && (
        <p
          className="text-sm text-kodo-red dark:text-kodo-red"
          role="alert"
          id={`${fieldId}-error`}
        >
          {error}
        </p>
      )}
    </div>
  );
}
