import React from "react";

interface ToggleSwitchProps {
  label: string;
  checked: boolean;
  onChange: () => void;
  disabled?: boolean;
  /**
   * The switch's accessible name when `label` is empty because the visible
   * title sits elsewhere in the card — four screens passed `label=""`, which
   * left the switch announced with no name at all.
   */
  ariaLabel?: string;
}

const Toggle: React.FC<ToggleSwitchProps> = ({
  label,
  checked,
  onChange,
  disabled,
  ariaLabel,
}) => {
  return (
    <div className="flex items-center gap-3">
      <span className="text-secondary-100 font-bold">{label}</span>
      <label className={`relative cursor-${disabled ? "default" : "pointer"}`}>
        <input
          type="checkbox"
          checked={checked}
          onChange={disabled ? undefined : onChange}
          /*
           * The visible `label` text is rendered as a sibling *outside* this
           * `<label>`, so it is not the control's accessible name. Naming it here
           * rather than restructuring the markup, which would change the layout.
           */
          role="switch"
          aria-checked={checked}
          aria-label={ariaLabel || label}
          className="sr-only"
          disabled={disabled}
        />
        <div
          className={`w-[36px] h-[20px] rounded-full shadow-inner transition duration-300 ${
            checked ? "bg-primary-5" : "bg-[#cbcbcb]"
          } ${disabled ? "opacity-50 cursor-not-allowed" : ""}`}
        />
        <div
          className={`absolute top-[2px] ${
            checked ? "left-[-3px]" : "left-[4px]"
          } w-4 h-4 bg-white rounded-full shadow-md transition-transform duration-300 ${
            checked ? "translate-x-5" : "translate-x-0"
          } ${disabled ? "cursor-not-allowed" : ""}`}
        />
      </label>
    </div>
  );
};

export default Toggle;
