"use client";

import React, { TextareaHTMLAttributes, useId } from "react";
import { AlertCircle } from "lucide-react";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/TextArea";
import { cn } from "@/lib/utils";

interface TextAreaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
  label?: string;
  error?: string;
  containerClass?: string;
  textareaClass?: string;
}

/**
 * Labelled multi-line field — the counterpart to `TextBox`.
 *
 * Composed from the shadcn `Textarea` and `Label` primitives; this wrapper owns
 * only the label/error wiring (`htmlFor`, `aria-describedby`, `role="alert"`).
 *
 * Renamed from `TextArea.tsx` so it no longer collides with the lowercase
 * `textarea.tsx` primitive on case-insensitive filesystems. The exported name
 * `TextArea` is unchanged, so call sites only needed their import path updated.
 */
export const TextArea: React.FC<TextAreaProps> = ({
  label,
  error,
  containerClass = "",
  textareaClass = "",
  id,
  ...props
}) => {
  const generatedId = useId();
  const textareaId = id ?? generatedId;
  const errorId = `${textareaId}-error`;

  return (
    <div className={cn("flex flex-col gap-2", containerClass)}>
      {label && (
        <Label htmlFor={textareaId} className="text-xs text-muted-foreground">
          {label}
        </Label>
      )}

      <Textarea
        id={textareaId}
        autoComplete="off"
        aria-invalid={Boolean(error)}
        aria-describedby={error ? errorId : undefined}
        className={cn("min-h-30", textareaClass)}
        {...props}
      />

      {error && (
        <p
          id={errorId}
          role="alert"
          className="flex items-center gap-1 text-xs font-medium text-destructive"
        >
          <AlertCircle className="size-3 shrink-0" aria-hidden />
          {error}
        </p>
      )}
    </div>
  );
};
