import { useState } from "react";
import { CLASSLENS_ICON } from "../lib/icon";

interface InviteGateProps {
  onSuccess: () => void;
  correctCode: string;
}

export function InviteGate({ onSuccess, correctCode }: InviteGateProps) {
  const [code, setCode] = useState("");
  const [error, setError] = useState("");

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    if (code.trim() === correctCode) {
      onSuccess();
    } else {
      setError("邀請碼不正確，請再試一次");
    }
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-[#0f1419] via-[#1a2332] to-[#0f1419]">
      <div className="relative">
        <div className="absolute -top-20 -left-20 w-40 h-40 bg-[var(--color-primary)]/20 rounded-full blur-3xl" />
        <div className="absolute -bottom-20 -right-20 w-40 h-40 bg-[var(--color-accent)]/20 rounded-full blur-3xl" />

        <div className="relative bg-[var(--color-surface)] border border-[var(--color-border)] rounded-2xl p-8 max-w-md w-full shadow-2xl">
          <div className="text-center mb-8">
            <img
              src={CLASSLENS_ICON}
              alt="ClassLens"
              className="w-24 h-24 mx-auto mb-4 rounded-2xl shadow-lg"
            />
            <h1 className="text-2xl font-bold text-[var(--color-text)] font-display">
              ClassLens
            </h1>
            <p className="text-[var(--color-text-muted)] mt-2">
              請輸入邀請碼以繼續
            </p>
          </div>

          <form onSubmit={handleSubmit} className="space-y-4">
            <div>
              <label className="block text-sm font-medium text-[var(--color-text-muted)] mb-2">
                邀請碼
              </label>
              <input
                type="text"
                value={code}
                onChange={(e) => { setCode(e.target.value); setError(""); }}
                placeholder="請輸入邀請碼..."
                required
                className="input text-center tracking-widest"
                autoFocus
              />
            </div>

            {error && (
              <p className="text-sm text-red-400 text-center flex items-center justify-center gap-1">
                <svg className="w-4 h-4 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                {error}
              </p>
            )}

            <button
              type="submit"
              className="w-full py-3 px-4 bg-gradient-to-r from-[var(--color-primary)] to-[var(--color-accent)] text-white font-semibold rounded-xl hover:opacity-90 transition-opacity"
            >
              進入
            </button>
          </form>
        </div>
      </div>
    </div>
  );
}
