import { Ref } from 'react';

import { cn } from '@/lib/helper';
import { TextInputProps } from '@/components/input/TextInput';

interface FileInputProps
  extends Omit<
    TextInputProps,
    | 'type'
    | 'value'
    | 'isValid'
    | 'startAdornment'
    | 'endAdornment'
    | 'isLoading'
  > {
  ref?: Ref<HTMLInputElement>;
  accept?: string;
  className?: {
    wrapper?: string;
    label?: string;
    input?: string;
  };
}

const FileInput = ({
  ref,
  label,
  bottomLabel,
  name,
  placeholder,
  accept = '*',
  className,
  isError,
  errorMessage,
  disabled = false,
  required = false,
  onChange,
  onBlur,
  readOnly = false,
}: FileInputProps) => {
  return (
    <div
      className={cn(
        'w-full flex flex-col gap-0 text-start rounded-lg',
        className?.wrapper
      )}
    >
      {label && (
        <label
          htmlFor={name}
          className={cn(
            'w-full py-2 text-xs font-semibold leading-5',
            {
              'text-error': isError,
            },
            className?.label
          )}
        >
          {label}
          {required && (
            <>
              <span className='tooltip tooltip-error' data-tip='required'>
                <span className='text-error'> *</span>
              </span>
            </>
          )}
        </label>
      )}

      <input
        ref={ref}
        type='file'
        accept={accept}
        id={name}
        name={name}
        placeholder={placeholder}
        onChange={onChange}
        onBlur={onBlur}
        disabled={disabled}
        className={cn(
          'grow file-input w-full h-fit px-3 py-1.5 text-sm font-normal leading-6 rounded-lg! outline-none! transition-all duration-200 bg-white border-base-content/10',
          className?.input
        )}
        readOnly={readOnly}
      />

      {!isError && bottomLabel && (
        <p className='w-full mt-1.5 text-xs opacity-60'>{bottomLabel}</p>
      )}
      {isError && errorMessage && (
        <p className='w-full mt-1.5 text-xs text-error'>{errorMessage}</p>
      )}
    </div>
  );
};

export default FileInput;
