import { Controller, Control, FieldError } from "react-hook-form";

interface FormInputProps {
  name: string;
  control: Control<any>;
  label?: string;
  type?: string;
  placeholder?: string;
  required?: boolean;
  disabled?: boolean;
  error?: FieldError;
  className?: string;
}

export default function FormInput({
  name,
  control,
  label = "",
  type = "text",
  placeholder,
  required,
  disabled,
  error,
  className = "",
}: FormInputProps) {
  return (
    <div>
      <label className="text-sm font-medium text-gray-700 mb-1 block">
        {label}
        {required && <span className="text-red-500">*</span>}
      </label>
      <Controller
        name={name}
        control={control}
        render={({ field }) => (
          <input
            {...field}
            type={type}
            placeholder={placeholder}
            disabled={disabled}
            className={`w-full h-[45px] border rounded-md px-4 py-2 outline-none transition-colors text-[14px] ${
              disabled
                ? "bg-gray-100 cursor-not-allowed text-gray-600 border-gray-200"
                : "focus:ring-1 focus:ring-teal-500 border-gray-300"
            } ${error ? "border-red-500" : ""} ${className}`}
          />
        )}
      />
      {error && (
        <p className="text-red-500 text-xs mt-1">{error.message}</p>
      )}
    </div>
  );
}
