"use client";

import { useState } from "react";
import { X, Copy, Check, Code, ExternalLink } from "lucide-react";

const t = {
  en: {
    title: "Embed Testimonials Widget",
    description: "Add this code to your website to display your testimonials.",
    iframeCode: "iFrame Embed Code",
    copyCode: "Copy Code",
    copied: "Copied!",
    preview: "Preview",
    width: "Width",
    height: "Height",
    openInNewTab: "Open in new tab",
  },
  ar: {
    title: "تضمين أداة الشهادات",
    description: "أضف هذا الكود إلى موقعك الإلكتروني لعرض شهاداتك.",
    iframeCode: "كود التضمين iFrame",
    copyCode: "نسخ الكود",
    copied: "تم النسخ!",
    preview: "معاينة",
    width: "العرض",
    height: "الارتفاع",
    openInNewTab: "فتح في تبويب جديد",
  },
};

interface EmbedCodeModalProps {
  lang: string;
  userId: string;
  onClose: () => void;
}

export default function EmbedCodeModal({
  lang,
  userId,
  onClose,
}: EmbedCodeModalProps) {
  const isAr = lang === "ar";
  const labels = t[isAr ? "ar" : "en"];

  const [copied, setCopied] = useState(false);
  const [width, setWidth] = useState("100%");
  const [height, setHeight] = useState("600");

  const origin = typeof window !== "undefined" ? window.location.origin : "";
  const embedUrl = `${origin}/${lang}/embed/testimonials/${userId}`;
  const embedCode = `<iframe src="${embedUrl}" width="${width}" height="${height}" frameborder="0" style="border:none;overflow:hidden;" loading="lazy"></iframe>`;

  const handleCopy = async () => {
    await navigator.clipboard.writeText(embedCode);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
      <div
        className="bg-white rounded-xl shadow-xl w-full max-w-lg"
        dir={isAr ? "rtl" : "ltr"}
      >
        <div className="flex items-center justify-between p-4 border-b">
          <div className="flex items-center gap-2">
            <Code className="w-5 h-5 text-brand-green" />
            <h3 className="text-lg font-semibold text-slate-900">
              {labels.title}
            </h3>
          </div>
          <button
            onClick={onClose}
            className="p-1 hover:bg-slate-100 rounded-lg"
          >
            <X className="w-5 h-5 text-slate-500" />
          </button>
        </div>

        <div className="p-4 space-y-4">
          <p className="text-sm text-slate-500">{labels.description}</p>

          {/* Size controls */}
          <div className="grid grid-cols-2 gap-3">
            <div>
              <label className="block text-xs text-slate-500 mb-1">
                {labels.width}
              </label>
              <input
                value={width}
                onChange={(e) => setWidth(e.target.value)}
                className="w-full border border-slate-300 rounded-lg px-3 py-1.5 text-slate-900 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
              />
            </div>
            <div>
              <label className="block text-xs text-slate-500 mb-1">
                {labels.height}
              </label>
              <input
                value={height}
                onChange={(e) => setHeight(e.target.value)}
                className="w-full border border-slate-300 rounded-lg px-3 py-1.5 text-slate-900 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
              />
            </div>
          </div>

          {/* Code block */}
          <div>
            <label className="block text-xs text-slate-500 mb-1">
              {labels.iframeCode}
            </label>
            <div className="relative">
              <pre className="bg-slate-50 border border-slate-200 rounded-lg p-3 text-xs text-slate-700 overflow-x-auto whitespace-pre-wrap break-all">
                {embedCode}
              </pre>
              <button
                onClick={handleCopy}
                className="absolute top-2 end-2 flex items-center gap-1 px-2 py-1 bg-slate-200 text-slate-600 rounded text-xs hover:bg-slate-300"
              >
                {copied ? (
                  <>
                    <Check className="w-3 h-3 text-green-600" />
                    {labels.copied}
                  </>
                ) : (
                  <>
                    <Copy className="w-3 h-3" />
                    {labels.copyCode}
                  </>
                )}
              </button>
            </div>
          </div>

          {/* Preview */}
          <div>
            <div className="flex items-center justify-between mb-1">
              <label className="text-xs text-slate-500">{labels.preview}</label>
              <a
                href={embedUrl}
                target="_blank"
                rel="noopener noreferrer"
                className="flex items-center gap-1 text-xs text-brand-green hover:text-brand-green/80"
              >
                <ExternalLink className="w-3 h-3" />
                {labels.openInNewTab}
              </a>
            </div>
            <div className="bg-white rounded-lg overflow-hidden border border-slate-200 h-48">
              <iframe
                src={embedUrl}
                width="100%"
                height="100%"
                style={{ border: "none" }}
                title="Testimonials Preview"
              />
            </div>
          </div>
        </div>

        <div className="p-4 border-t flex justify-end">
          <button
            onClick={onClose}
            className="px-4 py-2 border border-slate-300 text-slate-700 rounded-lg hover:bg-slate-50 text-sm"
          >
            {isAr ? "إغلاق" : "Close"}
          </button>
        </div>
      </div>
    </div>
  );
}
