// Auth screens — Login, Signup, Forgot password, Reset password, Verify email
function AuthShell({ children, mode, onNavigate }) {
  return (
    <div className="auth-shell">
      <aside className="auth-aside">
        <Brand />
        {/* The aside content is the honest "what you get" — no fake
            testimonials, no mock tickers. Everything below is true today. */}
        <div style={{ marginTop: 24, color: "var(--text-dim)", fontSize: 14, lineHeight: 1.6 }}>
          <div style={{ fontSize: 13, color: "var(--green)", fontFamily: "var(--ff-mono)", letterSpacing: "0.08em", textTransform: "uppercase", marginBottom: 14 }}>
            What you get
          </div>
          <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 12 }}>
            <li><Icon.Check style={{ width: 13, height: 13, color: "var(--green)", verticalAlign: "-2px", marginRight: 10 }} />Personal API key the moment you sign up</li>
            <li><Icon.Check style={{ width: 13, height: 13, color: "var(--green)", verticalAlign: "-2px", marginRight: 10 }} />3-day Pro trial — 10,000 requests / minute, no card required</li>
            <li><Icon.Check style={{ width: 13, height: 13, color: "var(--green)", verticalAlign: "-2px", marginRight: 10 }} />All 5 options endpoints (expirations, strikes, lookup, chain, quotes)</li>
            <li><Icon.Check style={{ width: 13, height: 13, color: "var(--green)", verticalAlign: "-2px", marginRight: 10 }} />Top 100 US underlyings supported</li>
            <li><Icon.Check style={{ width: 13, height: 13, color: "var(--green)", verticalAlign: "-2px", marginRight: 10 }} />Usage analytics built in</li>
          </ul>
          <div style={{ marginTop: 22, fontSize: 12.5, color: "var(--muted)", lineHeight: 1.55 }}>
            Data can be delayed up to 60 seconds. Every response carries an
            <code className="mono" style={{ margin: "0 4px", color: "var(--amber)" }}>X-Credits-Used</code>
            header so you always know what each call cost.
          </div>
        </div>
      </aside>
      <main className="auth-main">
        <div className="auth-card">
          {children}
        </div>
        <div className="auth-foot" style={{ position: "absolute", bottom: 24 }}>
          <span style={{ fontFamily: "var(--ff-mono)" }}>
            {mode === "login" && (<>New to Market-Options? <a onClick={() => onNavigate("signup")}>Create an account</a></>)}
            {mode === "signup" && (<>Already have an account? <a onClick={() => onNavigate("login")}>Sign in</a></>)}
            {mode === "forgot" && (<>Remembered? <a onClick={() => onNavigate("login")}>Back to sign in</a></>)}
            {mode === "reset" && (<>Remembered? <a onClick={() => onNavigate("login")}>Back to sign in</a></>)}
            {mode === "verify" && (<><a onClick={() => onNavigate("login")}>Back to sign in</a></>)}
          </span>
        </div>
      </main>
    </div>
  );
}

// Inline form error banner.
function AuthError({ message }) {
  if (!message) return null;
  return (
    <div style={{
      padding: "9px 12px", borderRadius: 7, fontSize: 12.5, lineHeight: 1.45,
      background: "rgba(229,72,77,0.12)", border: "1px solid var(--red)",
      color: "var(--red)",
    }}>
      {message}
    </div>
  );
}

function AuthSuccess({ message }) {
  if (!message) return null;
  return (
    <div style={{
      padding: "9px 12px", borderRadius: 7, fontSize: 12.5, lineHeight: 1.45,
      background: "var(--green-soft)", border: "1px solid var(--green)",
      color: "var(--green)",
    }}>
      {message}
    </div>
  );
}

// "Continue with Google" — renders Google's official button once the Google
// Identity Services script has loaded. Renders nothing until a Google client
// id is configured (window.GOOGLE_CLIENT_ID), so it is safe to ship early.
function GoogleSignInButton({ onNavigate, onError }) {
  const ref = useRef();
  const clientId = window.GOOGLE_CLIENT_ID;

  useEffect(() => {
    if (!clientId) return;
    let cancelled = false;
    let tries = 0;
    const tryInit = () => {
      if (cancelled) return;
      const gid = window.google && window.google.accounts && window.google.accounts.id;
      if (gid && ref.current) {
        gid.initialize({
          client_id: clientId,
          callback: (resp) => {
            if (!resp || !resp.credential) return;
            API.google(resp.credential)
              .then((data) => {
                Auth.setSession(data.token, data.user);
                onNavigate("dashboard");
              })
              .catch((err) => {
                if (onError) onError((err && err.message) || "Google sign-in failed.");
              });
          },
        });
        gid.renderButton(ref.current, {
          theme: "outline", size: "large", width: 320, text: "continue_with",
        });
      } else if (tries++ < 40) {
        setTimeout(tryInit, 100);
      }
    };
    tryInit();
    return () => { cancelled = true; };
  }, [clientId]);

  if (!clientId) return null;
  return (
    <div style={{ marginTop: 16 }}>
      <div className="row" style={{ alignItems: "center", gap: 10, marginBottom: 14 }}>
        <div style={{ flex: 1, height: 1, background: "var(--border)" }} />
        <span className="mono" style={{ fontSize: 11, color: "var(--muted)" }}>OR</span>
        <div style={{ flex: 1, height: 1, background: "var(--border)" }} />
      </div>
      <div ref={ref} style={{ display: "flex", justifyContent: "center", minHeight: 40 }} />
    </div>
  );
}

function Login({ onNavigate }) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);

  const submit = (e) => {
    e.preventDefault();
    if (loading) return;
    setError("");
    setLoading(true);
    API.post("/auth/login", { email: email, password: password })
      .then((data) => {
        Auth.setSession(data.token, data.user);
        onNavigate("dashboard");
      })
      .catch((err) => {
        setError(err.message || "Sign in failed. Please try again.");
        setLoading(false);
      });
  };

  return (
    <AuthShell mode="login" onNavigate={onNavigate}>
      <span className="eyebrow">Welcome back</span>
      <h1>Sign in to your console</h1>
      <p className="sub">Access keys, usage, and the chain endpoint.</p>
      <form onSubmit={submit}>
        <div className="field">
          <label>Email</label>
          <input className="input" type="email" value={email} required
            placeholder="you@company.com"
            onChange={(e) => setEmail(e.target.value)} />
        </div>
        <div className="field">
          <label style={{ display: "flex", justifyContent: "space-between" }}>
            <span>Password</span>
            <a onClick={() => onNavigate("forgot")} style={{ color: "var(--muted)", cursor: "pointer", fontSize: 12 }}>Forgot?</a>
          </label>
          <input className="input" type="password" value={password} required
            placeholder="Your password"
            onChange={(e) => setPassword(e.target.value)} />
        </div>
        <AuthError message={error} />
        <button type="submit" className="btn btn-primary" disabled={loading}
          style={{ justifyContent: "center", padding: "10px 14px" }}>
          {loading ? "Signing in…" : <>Sign in <Icon.ArrowRight style={{ width: 14, height: 14 }} /></>}
        </button>
      </form>
      <GoogleSignInButton onNavigate={onNavigate} onError={setError} />
    </AuthShell>
  );
}

function Signup({ onNavigate }) {
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [agreed, setAgreed] = useState(true);

  const strength = Math.min(4, Math.floor(password.length / 3));
  const strengthLabel =
    password.length === 0 ? "Use at least 8 characters"
    : password.length < 8 ? "Too short — use at least 8 characters"
    : strength >= 4 ? "Strong password"
    : "Decent — longer is stronger";

  const submit = (e) => {
    e.preventDefault();
    if (loading) return;
    setError("");
    if (password.length < 8) {
      setError("Password must be at least 8 characters long.");
      return;
    }
    if (!agreed) {
      setError("Please accept the Terms to continue.");
      return;
    }
    setLoading(true);
    const name = (firstName + " " + lastName).trim();
    API.post("/auth/register", { email: email, password: password, name: name })
      .then((data) => {
        Auth.setSession(data.token, data.user);
        onNavigate("dashboard");
      })
      .catch((err) => {
        setError(err.message || "Could not create your account.");
        setLoading(false);
      });
  };

  return (
    <AuthShell mode="signup" onNavigate={onNavigate}>
      <span className="eyebrow">3-day Pro trial · 10,000 requests / minute</span>
      <h1>Create your account</h1>
      <p className="sub">Your personal API key is issued the moment you sign up.</p>
      <form onSubmit={submit}>
        <div className="form-row">
          <div className="field">
            <label>First name</label>
            <input className="input" value={firstName}
              onChange={(e) => setFirstName(e.target.value)} placeholder="Emma" />
          </div>
          <div className="field">
            <label>Last name</label>
            <input className="input" value={lastName}
              onChange={(e) => setLastName(e.target.value)} placeholder="Larsson" />
          </div>
        </div>
        <div className="field">
          <label>Work email</label>
          <input className="input" type="email" value={email} required
            placeholder="you@company.com"
            onChange={(e) => setEmail(e.target.value)} />
        </div>
        <div className="field">
          <label>Password</label>
          <input className="input" type="password" value={password} required
            placeholder="At least 8 characters"
            onChange={(e) => setPassword(e.target.value)} />
          <div style={{ display: "flex", gap: 4, marginTop: 4 }}>
            {[0, 1, 2, 3].map((i) => (
              <span key={i} style={{
                flex: 1, height: 3, borderRadius: 2,
                background: i < strength ? "var(--green)" : "var(--border)",
              }} />
            ))}
          </div>
          <span className="mono" style={{ fontSize: 11, color: "var(--muted)" }}>{strengthLabel}</span>
        </div>
        <AuthError message={error} />
        <label style={{ display: "flex", gap: 10, alignItems: "flex-start", color: "var(--text-dim)", fontSize: 12.5, lineHeight: 1.5, cursor: "pointer" }}>
          <input type="checkbox" checked={agreed} onChange={(e) => setAgreed(e.target.checked)} style={{ marginTop: 3 }} />
          <span>I agree to the Terms of Service and Data License. Market-Options does not provide investment advice.</span>
        </label>
        <button type="submit" className="btn btn-primary" disabled={loading}
          style={{ justifyContent: "center", padding: "10px 14px" }}>
          {loading ? "Creating account…" : <>Create account &amp; issue key <Icon.ArrowRight style={{ width: 14, height: 14 }} /></>}
        </button>
      </form>
      <GoogleSignInButton onNavigate={onNavigate} onError={setError} />
    </AuthShell>
  );
}

/**
 * Forgot-password — asks for an email and calls the backend. The backend
 * always returns 200 (anti-enumeration) so the client message is the same
 * regardless of whether the email matched a real account.
 */
function Forgot({ onNavigate }) {
  const [email, setEmail] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [sent, setSent] = useState(false);

  const submit = (e) => {
    e.preventDefault();
    if (loading) return;
    setError("");
    if (!email) { setError("Enter your email."); return; }
    setLoading(true);
    API.forgotPassword(email)
      .then(() => { setSent(true); setLoading(false); })
      .catch((err) => {
        // Backend is configured to always succeed for anti-enumeration; an
        // error here means a network failure or a server fault, not a
        // missing email. Show it honestly.
        setError((err && err.message) || "Could not send the reset email. Please try again.");
        setLoading(false);
      });
  };

  return (
    <AuthShell mode="forgot" onNavigate={onNavigate}>
      <span className="eyebrow">Account recovery</span>
      <h1>Reset your password</h1>
      <p className="sub">
        Enter the email you signed up with. If it matches an account, we'll
        send a one-time reset link that expires in 60 minutes.
      </p>
      {sent ? (
        <div className="card" style={{ padding: 18, marginTop: 8 }}>
          <AuthSuccess message="If that email is registered, a reset link is on its way. Check your inbox (and spam folder) within a minute or two." />
          <div className="mono text-xs muted" style={{ marginTop: 14, lineHeight: 1.55 }}>
            Didn't get one? Make sure you typed the exact address you signed up with. Google-only accounts cannot be reset this way — sign in with Google instead.
          </div>
        </div>
      ) : (
        <form onSubmit={submit}>
          <div className="field">
            <label>Email</label>
            <input className="input" type="email" value={email} required autoFocus
              placeholder="you@company.com"
              onChange={(e) => setEmail(e.target.value)} />
          </div>
          <AuthError message={error} />
          <button type="submit" className="btn btn-primary" disabled={loading}
            style={{ justifyContent: "center", padding: "10px 14px" }}>
            {loading ? "Sending…" : <>Send reset link <Icon.ArrowRight style={{ width: 14, height: 14 }} /></>}
          </button>
        </form>
      )}
    </AuthShell>
  );
}

/**
 * Reset-password — landing target for the link in the reset email. Reads the
 * token from the URL (`?action=reset-password&token=...`), takes a new
 * password, calls the backend, and on success sends the user to sign in.
 * Successful reset also invalidates every existing session for that user.
 */
function ResetPassword({ onNavigate }) {
  const [token, setToken] = useState("");
  const [password, setPassword] = useState("");
  const [confirm, setConfirm] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [done, setDone] = useState(false);

  useEffect(() => {
    try {
      const params = new URLSearchParams(window.location.search);
      const t = params.get("token") || "";
      setToken(t);
      if (!t) setError("This reset link is incomplete. Open it from your email again, or request a new one.");
    } catch (e) { /* ignore */ }
  }, []);

  const submit = (e) => {
    e.preventDefault();
    if (loading) return;
    setError("");
    if (password.length < 8) { setError("Password must be at least 8 characters."); return; }
    if (password !== confirm) { setError("The two passwords do not match."); return; }
    if (!token) return;
    setLoading(true);
    API.resetPassword(token, password)
      .then(() => { setDone(true); setLoading(false); })
      .catch((err) => {
        setError((err && err.message) || "Could not reset your password. The link may have expired.");
        setLoading(false);
      });
  };

  return (
    <AuthShell mode="reset" onNavigate={onNavigate}>
      <span className="eyebrow">Account recovery</span>
      <h1>Choose a new password</h1>
      <p className="sub">
        Pick a new password for your account. Existing sessions on every
        device will be signed out the moment this completes.
      </p>
      {done ? (
        <div className="card" style={{ padding: 18, marginTop: 8 }}>
          <AuthSuccess message="Your password has been reset. You can now sign in with the new password." />
          <button className="btn btn-primary" onClick={() => onNavigate("login")}
            style={{ marginTop: 16, justifyContent: "center", padding: "10px 14px", width: "100%" }}>
            Continue to sign in <Icon.ArrowRight style={{ width: 14, height: 14 }} />
          </button>
        </div>
      ) : (
        <form onSubmit={submit}>
          <div className="field">
            <label>New password</label>
            <input className="input" type="password" value={password} required autoFocus
              placeholder="At least 8 characters"
              onChange={(e) => setPassword(e.target.value)} />
          </div>
          <div className="field">
            <label>Confirm new password</label>
            <input className="input" type="password" value={confirm} required
              placeholder="Repeat the new password"
              onChange={(e) => setConfirm(e.target.value)} />
          </div>
          <AuthError message={error} />
          <button type="submit" className="btn btn-primary" disabled={loading || !token}
            style={{ justifyContent: "center", padding: "10px 14px" }}>
            {loading ? "Resetting…" : <>Reset password <Icon.ArrowRight style={{ width: 14, height: 14 }} /></>}
          </button>
        </form>
      )}
    </AuthShell>
  );
}

/**
 * Verify-email — landing target for the link in the verification email. Runs
 * the verification call automatically on mount, then shows a clear success
 * or failure state with the right next-step button.
 */
function VerifyEmail({ onNavigate }) {
  const [state, setState] = useState("checking"); // checking | ok | fail | missing
  const [error, setError] = useState("");

  useEffect(() => {
    let token = "";
    try {
      const params = new URLSearchParams(window.location.search);
      token = params.get("token") || "";
    } catch (e) { /* ignore */ }
    if (!token) { setState("missing"); return; }
    API.verifyEmail(token)
      .then(() => setState("ok"))
      .catch((err) => {
        setError((err && err.message) || "This link is invalid or has expired.");
        setState("fail");
      });
  }, []);

  return (
    <AuthShell mode="verify" onNavigate={onNavigate}>
      <span className="eyebrow">Account verification</span>
      <h1>
        {state === "checking" && "Verifying your email…"}
        {state === "ok" && "Email verified"}
        {(state === "fail" || state === "missing") && "Verification failed"}
      </h1>
      <p className="sub">
        {state === "checking" && "One moment — confirming your link with the server."}
        {state === "ok" && "Your email is now confirmed. You can keep using your account as normal."}
        {state === "missing" && "This link is incomplete. Open it from your email again, or request a new verification link from the dashboard."}
        {state === "fail" && (error || "This link is invalid or has expired.")}
      </p>
      <div style={{ marginTop: 18, display: "flex", gap: 10 }}>
        {state === "ok" && (
          <button className="btn btn-primary" onClick={() => onNavigate(Auth.isAuthed() ? "dashboard" : "login")}
            style={{ justifyContent: "center", padding: "10px 14px" }}>
            {Auth.isAuthed() ? "Back to dashboard" : "Sign in"} <Icon.ArrowRight style={{ width: 14, height: 14 }} />
          </button>
        )}
        {(state === "fail" || state === "missing") && (
          <button className="btn" onClick={() => onNavigate("login")}
            style={{ justifyContent: "center", padding: "10px 14px" }}>
            Back to sign in
          </button>
        )}
      </div>
    </AuthShell>
  );
}

Object.assign(window, { Login, Signup, Forgot, ResetPassword, VerifyEmail });
