import React from "react";
import { GoogleLogin } from "@react-oauth/google";

export default function Login({ onDone }) {
  return (
    <div style={{ display: "grid", placeItems: "center", minHeight: "100vh" }}>
      <div className="panel" style={{ maxWidth: 460, width: "92%" }}>
        <div className="panel-head">
          <div className="panel-title">Sign in to netIntel</div>
        </div>
        <div className="panel-body">
          <GoogleLogin
            onSuccess={async (cred) => {
              const r = await fetch("/api/auth/google", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                credentials: "include",
                body: JSON.stringify({ credential: cred.credential }),
              });
              if (r.ok) onDone?.();
              else console.error(await r.json());
            }}
            onError={() => console.error("Google login failed")}
          />
        </div>
      </div>
    </div>
  );
}
