"use client";

import { useState } from "react";
import { useTranslation } from "react-i18next";

import type { ChoiceValue } from "@/lib/choices";

/**
 * Generic over the id for the same reason as `SelectInput`: the profile forms
 * key interests on the numeric choice id, the volunteer-opportunity form on
 * `String(id)`. The `string` default leaves existing call sites unchanged.
 */
interface TagsCheckboxProps<I extends ChoiceValue = string> {
  name: string;
  label: string;
/* `unknown` rather than `any`: these components only *write* field values,
     never read them back, and Formik's own `setFieldValue` accepts anything.
     Anything is assignable to `unknown`, so every caller is unaffected. */
  setFieldValue: (
    field: string,
    value: unknown,
    shouldValidate?: boolean
  ) => void;
  setFieldTouched: (
    field: string,
    isTouched?: boolean,
    shouldValidate?: boolean,
  ) => void;
  values: I[] | undefined;
  errors?: string | string[];
  touched?: boolean;
  notTransparent?: boolean;
  options: { id: I; label: string }[];
  disabled?: boolean;
}

export function TagsCheckbox<I extends ChoiceValue = string>({
  name,
  setFieldValue,
  setFieldTouched,
  values = [] as I[], // Default to empty array if undefined
  errors,
  touched,
  notTransparent = false,
  options,
  disabled,
}: TagsCheckboxProps<I>) {
  const [, setIsFocused] = useState(false);
  const { t } = useTranslation();

  const handleCheckboxChange = (option: I) => {
    const newValues = values.includes(option)
      ? values.filter((value) => value !== option)
      : [...values, option];

    setFieldValue(name, newValues, false);
    setTimeout(() => {
      setFieldTouched(name, true, true);
    }, 0);
  };

  const handleBlur = () => {
    setIsFocused(false);
    setFieldTouched(name, true, true);
  };

  return (
    <div className="relative w-full mb-4">
      <div
        className={`flex flex-wrap gap-y-[14px] gap-x-[5px] px-3 pb-6 pt-5 border rounded-2xl min-h-[48px] items-center
          ${notTransparent ? "bg-white" : "bg-[#29246D]/[0.03]"}
          ${touched && errors ? "border-red-500" : "border-[#29246D1A]/10"}`}
        onFocus={() => setIsFocused(true)}
        onBlur={handleBlur}
      >
        <span className="text-primary-5 transition-all duration-200 placeholder-primary-5 lg:text-base md:text-sm text-sm pr-3">
          {t("COMMON.ENTER_TAGS")}
        </span>
        {options.map((option, index) => (
          <label
            key={index}
            className={`flex items-center px-3 py-2 rounded-full text-sm font-medium cursor-pointer transition-colors focus-within:ring-2 focus-within:ring-primary-5/50 focus-within:ring-offset-1
              ${
                values.includes(option.id)
                  ? "bg-primary-5 text-white" // Blue background when checked
                  : "bg-white text-primary-5 border border-primary-5" // White background when unchecked
              }`}
          >
            <input
              type="checkbox"
              value={option.id}
              checked={values.includes(option.id)}
              onChange={() => handleCheckboxChange(option.id)}
              /*
               * `sr-only`, not `hidden`: `display: none` takes the input out of
               * the accessibility tree and out of the tab order, so the tag could
               * only ever be set with a mouse. `sr-only` is absolutely positioned
               * and clipped, so it stays invisible and stays focusable — the
               * visible pill is the `<label>` around it either way.
               */
              className="sr-only"
              disabled={disabled}
            />
            <span className="text-[15px]">{option.label}</span>
          </label>
        ))}
      </div>
      {touched && errors && (
        <div className="text-red-500 text-sm mt-1">{errors}</div>
      )}
    </div>
  );
}

export default TagsCheckbox;
