"use client"

import * as React from "react"
import { cn } from "@/lib/utils"

export interface FormInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  label?: string
  error?: string
  helperText?: string
}

const FormInput = React.forwardRef<HTMLInputElement, FormInputProps>(
  ({ className, type, label, error, helperText, id, ...props }, ref) => {
    const generatedId = React.useId()
    const inputId = id || generatedId

    return (
      <div className="space-y-2">
        {label ? (
          <label htmlFor={inputId} className="text-sm font-semibold leading-none text-foreground">
            {label}
          </label>
        ) : null}
        <div className="relative">
          <input
            id={inputId}
            type={type}
            data-slot="input"
            className={cn(
              "h-12 w-full min-w-0 rounded-xl border border-border/70 bg-white px-4 text-base text-foreground shadow-sm transition outline-none",
              "placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground",
              "focus:border-primary focus:ring-4 focus:ring-primary/15 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
              error ? "border-destructive focus:ring-destructive/10" : "",
              className,
            )}
            ref={ref}
            {...props}
          />
        </div>
        {error ? <p className="text-sm text-destructive">{error}</p> : null}
        {helperText && !error ? <p className="text-sm text-muted-foreground">{helperText}</p> : null}
      </div>
    )
  },
)
FormInput.displayName = "FormInput"

export { FormInput }
