"use client";

import { CheckIcon, CopyIcon, ExternalLinkIcon } from "lucide-react";
import Link from "next/link";
import { useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { Webhook } from "@formbricks/database/prisma-browser";
import { copyToClipboard } from "@/lib/utils/clipboard";
import { Button } from "@/modules/ui/components/button";
import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/modules/ui/components/dialog";
import { Input } from "@/modules/ui/components/input";
import { Label } from "@/modules/ui/components/label";

interface WebhookCreatedModalProps {
  readonly open: boolean;
  readonly webhook: Webhook;
  readonly onClose: () => void;
}

export const WebhookCreatedModal = ({ open, webhook, onClose }: WebhookCreatedModalProps) => {
  const { t } = useTranslation();
  const [copied, setCopied] = useState(false);

  const handleCopySecret = async (text: string) => {
    if (!(await copyToClipboard(text))) {
      toast.error(t("common.failed_to_copy_to_clipboard"));
      return;
    }
    setCopied(true);
    toast.success(t("common.copied_to_clipboard"));
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <Dialog open={open} onOpenChange={onClose}>
      <DialogContent>
        <DialogHeader>
          <CheckIcon className="size-6 text-green-500" />
          <DialogTitle>{t("workspace.integrations.webhooks.webhook_created")}</DialogTitle>
          <DialogDescription>{t("workspace.integrations.webhooks.copy_secret_now")}</DialogDescription>
        </DialogHeader>

        <DialogBody className="space-y-4 pb-4">
          <div className="col-span-1">
            <Label>{t("workspace.integrations.webhooks.signing_secret")}</Label>
            <div className="mt-1 flex">
              <Input type="text" readOnly value={webhook.secret ?? ""} className="font-mono text-sm" />
              <Button
                type="button"
                variant="secondary"
                className="ml-2 whitespace-nowrap"
                onClick={() => handleCopySecret(webhook.secret ?? "")}>
                {copied ? (
                  <>
                    <CheckIcon className="size-4" />
                    {t("common.copied")}
                  </>
                ) : (
                  <>
                    <CopyIcon className="size-4" />
                    {t("common.copy")}
                  </>
                )}
              </Button>
            </div>
            <p className="mt-2 text-xs text-slate-500">
              {t("workspace.integrations.webhooks.secret_copy_warning")}
            </p>
            <Link
              href="https://formbricks.com/docs/platform/features/integrations/webhooks#webhook-security-with-standard-webhooks"
              target="_blank"
              className="mt-2 inline-flex items-center gap-1 text-xs text-slate-600 underline hover:text-slate-800">
              {t("workspace.integrations.webhooks.learn_to_verify")}
              <ExternalLinkIcon className="size-3" />
            </Link>
          </div>
        </DialogBody>

        <DialogFooter>
          <Button type="button" onClick={onClose}>
            {t("common.done")}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};
