"use client";

import { CopyIcon, Trash2Icon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { TSurveyQuota } from "@formbricks/types/quota";
import { Button } from "@/modules/ui/components/button";
import { Label } from "@/modules/ui/components/label";
import { TooltipRenderer } from "@/modules/ui/components/tooltip";

interface QuotaListProps {
  readonly quotas: TSurveyQuota[];
  readonly onEdit: (quota: TSurveyQuota) => void;
  readonly deleteQuota: (quota: TSurveyQuota) => void;
  readonly duplicateQuota: (quota: TSurveyQuota) => void;
}

export const QuotaList = ({ quotas, onEdit, deleteQuota, duplicateQuota }: QuotaListProps) => {
  const { t } = useTranslation();

  return (
    <div className="space-y-3">
      {quotas.map((quota) => (
        // Using div instead of button to avoid nested button HTML validation errors
        <div
          key={quota.id}
          className="flex w-full cursor-pointer items-center justify-between rounded-lg bg-slate-50 p-4 transition-colors hover:bg-slate-100"
          onClick={() => onEdit(quota)}
          onKeyDown={(e) => {
            // Ignore keys bubbling up from the nested delete/duplicate buttons so
            // Enter/Space there doesn't also trigger edit.
            if (e.target !== e.currentTarget) return;
            const isActivationKey = e.key === "Enter" || e.key === " ";
            if (isActivationKey) {
              e.preventDefault();
              onEdit(quota);
            }
          }}
          // eslint-disable-next-line jsx-a11y/prefer-tag-over-role -- a <button> would nest the delete and duplicate buttons inside it (see above)
          role="button"
          tabIndex={0}>
          <div className="text-left">
            <Label className="text-sm font-medium text-slate-800">{quota.name}</Label>
            <div className="mt-1 text-sm text-slate-500">
              {t("workspace.surveys.edit.quotas.limited_to_x_responses", {
                limit: quota.limit.toLocaleString(),
              })}
            </div>
          </div>

          <div className="flex items-center gap-2">
            <TooltipRenderer tooltipContent={t("common.delete")}>
              <Button
                variant="ghost"
                size="sm"
                onClick={(e) => {
                  e.stopPropagation();
                  deleteQuota(quota);
                }}
                className="size-8 p-0 text-slate-500">
                <Trash2Icon className="size-4" />
              </Button>
            </TooltipRenderer>
            <TooltipRenderer tooltipContent={t("common.duplicate")}>
              <Button
                variant="ghost"
                size="sm"
                onClick={(e) => {
                  e.stopPropagation();
                  duplicateQuota(quota);
                }}
                className="size-8 p-0 text-slate-500">
                <CopyIcon className="size-4" />
              </Button>
            </TooltipRenderer>
          </div>
        </div>
      ))}
    </div>
  );
};
