import * as React from 'react'
import { cva, type VariantProps } from 'class-variance-authority'
import { cn } from '@/lib/utils'

const textareaVariants = cva(
  [
    'flex w-full rounded-lg border border-border-tertiary bg-bg-container text-text transition-colors',
    'placeholder:text-text-tertiary',
    'outline-none focus:outline-none focus:ring-0 focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0',
    'focus:border-border-secondary focus-visible:border-border-secondary',
    'disabled:cursor-not-allowed disabled:opacity-50',
    'aria-invalid:border-error aria-invalid:ring-0',
  ].join(' '),
  {
    variants: {
      size: {
        sm: 'min-h-[var(--textarea-min-height-sm)] px-[var(--control-padding-x-sm)] py-[var(--control-padding-y-sm)] text-[length:var(--control-font-size-sm)] leading-[var(--control-line-height-sm)]',
        md: 'min-h-[var(--textarea-min-height-md)] px-[var(--control-padding-x-md)] py-[var(--control-padding-y-md)] text-[length:var(--control-font-size-md)] leading-[var(--control-line-height-md)]',
        lg: 'min-h-[var(--textarea-min-height-lg)] px-[var(--control-padding-x-lg)] py-[var(--control-padding-y-lg)] text-[length:var(--control-font-size-lg)] leading-[var(--control-line-height-lg)]',
      },
    },
    defaultVariants: { size: 'md' },
  }
)

export interface TextareaProps
  extends Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, 'size'>,
    VariantProps<typeof textareaVariants> {
  size?: 'sm' | 'md' | 'lg'
}

const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
  ({ className, size = 'md', ...props }, ref) => (
    <textarea
      ref={ref}
      className={cn(textareaVariants({ size }), className)}
      {...props}
    />
  ),
)
Textarea.displayName = 'Textarea'

export { Textarea, textareaVariants }
