import { cn } from "@/lib/utils";
import {
  HTMLInputAutoCompleteAttribute,
  type HTMLInputTypeAttribute,
  useId,
} from "react";
import {
  type FieldErrors,
  type FieldValues,
  type Path,
  type UseFormRegister,
  type UseFormRegisterReturn,
} from "react-hook-form";
import { get } from "react-hook-form";

type InputGroupProps<T extends FieldValues> = {
  name: Path<T>;
  label: string;
  type: HTMLInputTypeAttribute;
  accept?: string;
  placeholder?: string;
  className?: string;
  fileStyleVariant?: "style1" | "style2";
  required?: boolean;
  disabled?: boolean;
  active?: boolean;
  handleChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
  value?: string;
  icon?: React.ReactNode;
  iconPosition?: "left" | "right";
  height?: "sm" | "default";
  autoComplete?: HTMLInputAutoCompleteAttribute;
  defaultValue?: string;
  dataCy?: string;
  register?: UseFormRegister<T>;
  errors?: FieldErrors<T>;
} & Partial<UseFormRegisterReturn>;

const InputGroup = <T extends FieldValues>({
  className,
  label,
  type,
  placeholder,
  accept,
  required,
  disabled,
  active,
  handleChange,
  icon,
  name,
  register,
  errors,
  autoComplete,
  dataCy,
  onChange,
  onBlur,
  ref,
  ...props
}: InputGroupProps<T>): React.ReactElement => {
  const id = useId();

  const error = errors && get(errors, name);

  return (
    <div className={className}>
      <label
        htmlFor={id}
        className="text-body-sm font-semibold capitalize text-dark dark:text-white"
      >
        {label}
        {required && <span className="ml-1 select-none text-error">*</span>}
      </label>

      <div
        className={cn(
          "relative mt-3 [&_svg]:pointer-events-none [&_svg]:absolute [&_svg]:top-1/2 [&_svg]:-translate-y-1/2 [&_svg]:text-dark-5 dark:[&_svg]:text-white/75",
          props.iconPosition === "left"
            ? "[&_svg]:left-4.5"
            : "[&_svg]:right-4.5",
        )}
      >
        <input
          id={id}
          type={type}
          accept={accept}
          placeholder={placeholder}
          name={name}
          ref={ref}
          autoComplete={autoComplete}
          data-cy={dataCy}
          onChange={(e) => {
            onChange?.(e);
            handleChange?.(e);
          }}
          onBlur={onBlur}
          {...(!onChange &&
            props.value !== undefined && { value: props.value })}
          {...(!onChange &&
            props.defaultValue !== undefined && {
              defaultValue: props.defaultValue,
            })}
          className={cn(
            "w-full rounded-xl border border-stroke/70 bg-white/90 shadow-[0_1px_2px_rgba(16,24,40,0.04),0_8px_20px_rgba(16,24,40,0.04)] outline-none backdrop-blur-[1px] transition-all duration-200 focus:border-primary/70 focus:bg-white focus:shadow-[0_0_0_2px_rgba(59,130,246,0.14),0_10px_24px_rgba(59,130,246,0.08)] disabled:cursor-default disabled:bg-gray-2/70 data-[active=true]:border-primary/70 dark:border-dark-3/80 dark:bg-dark-2/85 dark:focus:border-primary/80 dark:focus:shadow-[0_0_0_2px_rgba(59,130,246,0.2),0_10px_24px_rgba(15,23,42,0.45)] dark:disabled:bg-dark dark:data-[active=true]:border-primary/80",
            error &&
              "focus:border-red border-none focus:shadow-[0_0_0_2px_rgba(239,68,68,0.16),0_10px_24px_rgba(239,68,68,0.1)] dark:border-none",
            type === "file"
              ? getFileStyles(props.fileStyleVariant!)
              : "px-5.5 py-3 text-dark placeholder:text-dark-6 dark:text-white",
            props.iconPosition === "left" && "pl-12.5",
            props.height === "sm" && "py-2.5",
          )}
          disabled={disabled}
          data-active={active}
        />

        {icon}
      </div>

      {error && (
        <p className="mt-1.5 text-sm font-medium text-error">
          {error.message as string}
        </p>
      )}
    </div>
  );
};

export default InputGroup;

function getFileStyles(variant: "style1" | "style2") {
  switch (variant) {
    case "style1":
      return `file:mr-5 file:border-collapse file:cursor-pointer file:border-0 file:border-r file:border-solid file:border-stroke file:bg-[#E2E8F0] file:px-6.5 file:py-[13px] file:text-body-sm file:font-medium file:text-dark-5 file:hover:bg-primary file:hover:bg-opacity-10 dark:file:border-dark-3 dark:file:bg-white/30 dark:file:text-white`;
    default:
      return `file:mr-4 file:rounded file:border-[0.5px] file:border-stroke file:bg-stroke file:px-2.5 file:py-1 file:text-body-xs file:font-medium file:text-dark-5 file:focus:border-primary dark:file:border-dark-3 dark:file:bg-white/30 dark:file:text-white px-3 py-[9px]`;
  }
}
