import { type ChangeEvent, type Dispatch, type InputHTMLAttributes, type SetStateAction } from "react";
import type { CreateRecipeFormDirtyEntries } from "../../pages/Create";

interface RecipeCreateFormInputProps
  extends Omit<
    InputHTMLAttributes<HTMLInputElement>,
    "value" | "onChange" | "name" | "type" | "placeholder" | "required"
  > {
  label: string;
  name: string; // ENSURE THE NAME MATCHES THE VALUE IN THE ENTRIES TYPE
  desc: string;
  placeholder: string;
  type?: string;
  required?: boolean;
  valid: boolean;
  value: string;
  setValue: Dispatch<SetStateAction<string>>;
  setDirty: Dispatch<SetStateAction<CreateRecipeFormDirtyEntries>>;
  error: string;
  parentClasses?: string;
  classes: string;
};

export default function RecipeCreateFormInput({ label, name, desc, placeholder, type = "text", required = false, valid, value, setDirty, setValue, error, parentClasses = "", classes, ...inputProps }: RecipeCreateFormInputProps) {
  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    setDirty(prev => ({ ...prev, [name]: true }));
    setValue(e.target.value);
  }

  return (
    <div className={`flex flex-col ${parentClasses}`}>
      <label htmlFor={name} className="text-sm">
        {label}
        {required && <span className="text-red-500">*</span>}
      </label>
      <p className="text-xs pt-1 pb-2 text-gray-700">
        {desc}
      </p>
      <input
        className={`${!valid ? "border-red-500" : ""} ${classes}`}
        type={type}
        name={name}
        value={value}
        onChange={handleChange}
        required={required}
        placeholder={placeholder}
        {...inputProps}
      />
      {!valid && (
        <p className="text-xs text-red-500 my-1">
          {error}
        </p>
      )}
    </div>
  );
}


