"use client";

import { forwardRef, type InputHTMLAttributes, type ReactNode } from "react";

interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
  label?: string;
  error?: string;
  leftIcon?: ReactNode;
  rightIcon?: ReactNode;
}

const Input = forwardRef<HTMLInputElement, InputProps>(
  ({ label, error, leftIcon, rightIcon, className = "", ...props }, ref) => {
    return (
      <div className="w-full">
        {label && (
          <label className="mb-1.5 block text-sm font-medium text-[var(--text-secondary)]">
            {label}
          </label>
        )}
        <div
          className={`flex items-center gap-3 rounded-xl border bg-[var(--input-bg)] px-3 py-2.5 transition focus-within:border-[var(--accent)] focus-within:ring-1 focus-within:ring-[var(--accent)]/20 ${
            error ? "border-red-500" : "border-[var(--border)]"
          }`}
        >
          {leftIcon && (
            <span className="flex shrink-0 text-[var(--text-muted)]">{leftIcon}</span>
          )}
          <input
            ref={ref}
            className={`w-full bg-transparent text-sm text-[var(--text-primary)] outline-none placeholder:text-[var(--text-muted)] disabled:opacity-60 ${className}`}
            {...props}
          />
          {rightIcon && (
            <span className="flex shrink-0 text-[var(--text-muted)]">{rightIcon}</span>
          )}
        </div>
        {error && <p className="mt-1 text-xs text-red-500">{error}</p>}
      </div>
    );
  }
);

Input.displayName = "Input";

export default Input;
