"use client";

import { TextareaHTMLAttributes, forwardRef } from "react";

interface TextareaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
  label?: string;
  error?: string;
  showCharCount?: boolean;
  maxLength?: number;
}

const Textarea = forwardRef<HTMLTextAreaElement, TextareaProps>(
  (
    { label, error, showCharCount, maxLength, className = "", value, ...props },
    ref
  ) => {
    const charCount = typeof value === "string" ? value.length : 0;

    return (
      <div>
        {label && (
          <label
            htmlFor={props.id}
            className="block text-sm font-semibold text-gray-300 mb-2 uppercase tracking-wider"
          >
            {label}
          </label>
        )}
        <textarea
          ref={ref}
          maxLength={maxLength}
          value={value}
          className={`w-full px-4 py-3 border rounded focus:outline-none transition resize-none ${className}`}
          style={{
            backgroundColor: "var(--input)",
            borderColor: "var(--border)",
            color: "var(--foreground)",
          }}
          onFocus={(e) => {
            e.target.style.borderColor = "var(--accent-color)";
          }}
          onBlur={(e) => {
            e.target.style.borderColor = "var(--border)";
          }}
          {...props}
        />
        {showCharCount && maxLength && (
          <p className="text-gray-500 text-xs mt-1 text-right">
            {charCount}/{maxLength} caractères
          </p>
        )}
        {error && <p className="text-red-400 text-xs mt-1">{error}</p>}
      </div>
    );
  }
);

Textarea.displayName = "Textarea";

export default Textarea;
