"use client";

import { Check, X, RefreshCcw } from "lucide-react";
import type { Lang } from "@/lib/config";
import type { SignupFormData, WaitlistTranslations } from "./types";

interface SignupModalProps {
  lang: Lang;
  t: WaitlistTranslations;
  formData: SignupFormData;
  onFormChange: (data: SignupFormData) => void;
  onSubmit: (e: React.FormEvent) => void;
  onClose: () => void;
  submitting: boolean;
  error: string | null;
  success: string | null;
}

export function SignupModal({
  lang,
  t,
  formData,
  onFormChange,
  onSubmit,
  onClose,
  submitting,
  error,
  success,
}: SignupModalProps) {
  const isAr = lang === "ar";

  return (
    <div className="fixed inset-0 bg-slate-900/20 backdrop-blur-sm flex items-center justify-center z-[100] p-4">
      <div
        className="bg-white rounded-2xl shadow-2xl max-w-md w-full overflow-hidden border border-slate-200"
        dir={isAr ? "rtl" : "ltr"}
      >
        <div className="flex items-center justify-between p-4 border-b border-slate-200">
          <h2 className="text-lg font-bold text-slate-900">{t.joinWaitlist}</h2>
          <button
            type="button"
            aria-label="Close"
            onClick={onClose}
            className="p-2 hover:bg-slate-100 rounded-lg transition-colors"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        <form onSubmit={onSubmit} className="p-4 space-y-4">
          {error && (
            <div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg text-sm">
              {error}
            </div>
          )}
          {success && (
            <div className="bg-emerald-50 border border-emerald-200 text-emerald-700 px-4 py-3 rounded-lg text-sm flex items-center gap-2">
              <Check className="w-5 h-5" />
              {success}
            </div>
          )}

          <div>
            <label className="block text-sm font-medium text-slate-700 mb-1">
              {t.name} *
            </label>
            <input
              type="text"
              required
              value={formData.name}
              onChange={(e) =>
                onFormChange({ ...formData, name: e.target.value })
              }
              className="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-green focus:border-transparent"
            />
          </div>

          <div>
            <label className="block text-sm font-medium text-slate-700 mb-1">
              {t.email} *
            </label>
            <input
              type="email"
              required
              value={formData.email}
              onChange={(e) =>
                onFormChange({ ...formData, email: e.target.value })
              }
              className="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-green focus:border-transparent"
            />
          </div>

          <div>
            <label className="block text-sm font-medium text-slate-700 mb-1">
              {t.mobile} *
            </label>
            <input
              type="tel"
              required
              value={formData.mobile}
              onChange={(e) =>
                onFormChange({ ...formData, mobile: e.target.value })
              }
              className="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-brand-green focus:border-transparent"
              placeholder="+974 5555 5555"
              dir="ltr"
            />
          </div>

          <div className="flex gap-3 pt-4">
            <button
              type="button"
              onClick={onClose}
              className="flex-1 px-4 py-2.5 border border-slate-200 text-slate-700 rounded-xl hover:bg-slate-50 transition-colors font-medium"
            >
              {t.cancel}
            </button>
            <button
              type="submit"
              disabled={submitting || !!success}
              className="flex-1 px-4 py-2.5 bg-brand-green text-white rounded-xl hover:bg-brand-greenHover disabled:opacity-50 transition-colors font-semibold"
            >
              {submitting ? (
                <RefreshCcw className="w-5 h-5 animate-spin mx-auto" />
              ) : (
                t.joinWaitlist
              )}
            </button>
          </div>
        </form>
      </div>
    </div>
  );
}
