"use client";

import Image from "next/image";
import { Download } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";

export default function CertificatePreviewModal({ src, title, onClose, onDownload, downloading }: {
  src: string;
  title: string;
  onClose: () => void;
  onDownload: () => void;
  downloading: boolean;
}) {
  const { t } = useTranslation();
  return (
    <Modal open onClose={onClose} title={t("COMMON.CERTIFICATE_PREVIEW")} size="full"
      className="certificate-preview-dialog" style={{ padding: "12px" }} disableFilterModalClass>
      {title && <p className="mb-4 text-center text-sm font-semibold text-primary-5 sm:text-base">{title}</p>}
      <div className="relative h-[min(60dvh,640px)] w-full overflow-hidden rounded-xl border border-primary-5/10 bg-[#F7F7FB]">
        {/\.html?(?:$|[?#])/i.test(src) ? (
          <iframe src={src} title={title || t("COMMON.CERTIFICATE_PREVIEW")} className="h-full w-full border-0" />
        ) : (
          <Image src={src} alt={title || t("COMMON.CERTIFICATE_PREVIEW")} fill unoptimized className="object-contain p-2 sm:p-4" />
        )}
      </div>
      <div className="mt-4 flex justify-center">
        <Button type="button" variant="primary" size="medium" onClick={onDownload} loading={downloading} disabled={downloading} className="gap-2 !rounded-full">
          <Download className="h-4 w-4" aria-hidden="true" />
          {t("COMMON.DOWNLOAD")}
        </Button>
      </div>
    </Modal>
  );
}
