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("Incorrect password — please try again.");
    }
  };

  return (
    <div className="min-h-screen flex items-center justify-center bg-[var(--color-background)] px-4">
      <div
        className="fixed top-0 left-0 right-0 h-1"
        style={{ background: "var(--color-primary)" }}
      />

      <div className="w-full max-w-sm">
        <div className="text-center mb-8">
          <img
            src={CLASSLENS_ICON}
            alt="ClassLens"
            className="w-16 h-16 mx-auto mb-4 rounded-xl shadow-sm"
          />
          <h1 className="font-display text-3xl font-bold text-[var(--color-primary)]">
            ClassLens
          </h1>
          <p className="text-sm text-[var(--color-text-muted)] mt-1">
            AI-Powered Exam Analysis
          </p>
        </div>

        <div className="card p-8">
          <form onSubmit={handleSubmit} className="space-y-4">
            <div>
              <label className="block text-sm font-medium text-[var(--color-text)] mb-1.5">
                Access password
              </label>
              <input
                type="password"
                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-500 flex items-center gap-1.5 bg-red-50 border border-red-200 rounded-lg px-3 py-2">
                <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 btn btn-primary py-3 text-base mt-2"
            >
              Enter
            </button>
          </form>
        </div>
      </div>
    </div>
  );
}
