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

const inputVariants = cva(
  [
    'flex w-full min-w-0 rounded-lg border border-border-tertiary bg-bg-container text-text transition-colors',
    'placeholder:text-text-tertiary',
    'file:border-0 file:bg-transparent file:font-medium file:text-text',
    'outline-none focus:outline-none focus:ring-0 focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0',
    'hover:border-border-secondary 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: 'h-[var(--control-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)] file:text-[length:var(--control-font-size-sm)]',
        md: 'h-[var(--control-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)] file:text-[length:var(--control-font-size-md)]',
        lg: 'h-[var(--control-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)] file:text-[length:var(--control-font-size-lg)]',
      },
    },
    defaultVariants: { size: 'md' },
  }
)

export interface InputProps
  extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'>,
    VariantProps<typeof inputVariants> {}

const Input = React.forwardRef<HTMLInputElement, InputProps>(
  ({ className, type, size = 'md', ...props }, ref) => (
    <input
      type={type}
      ref={ref}
      className={cn(inputVariants({ size }), className)}
      {...props}
    />
  ),
)
Input.displayName = 'Input'

export { Input, inputVariants }
