import React, { useEffect, useMemo, useState } from "react";

import { Box, Button, Paper, Typography } from "@mui/material";

import { api } from "../api";
import { clearAuthToken, getAuthToken } from "../auth";
import { useToast } from "../toast";

type MeResponse = {
  provider?: string;
  id?: string;
  email?: string;
  userName?: string;
  firstName?: string;
  lastName?: string;
  displayName?: string;
  roles?: string[];
  googleLink?: {
    linked: boolean;
    email?: string | null;
  } | null;
};

export default function AuthStatusCard() {
  const { toast } = useToast();
  const token = getAuthToken();
  const [me, setMe] = useState<MeResponse | null>(null);

  useEffect(() => {
    if (!token) {
      setMe(null);
      return;
    }
    api
      .get<MeResponse>("/auth/me")
      .then((r) => setMe(r.data))
      .catch(() => setMe(null));
  }, [token]);

  const label = useMemo(() => me?.displayName || [me?.firstName, me?.lastName].filter(Boolean).join(" ") || me?.email, [me]);

  return (
    <Paper sx={{ mt: 2, p: 2 }}>
      <Typography variant="h6" sx={{ mb: 1 }}>
        Authentication
      </Typography>

      {!token ? (
        <Typography sx={{ color: "text.secondary" }}>Not signed in.</Typography>
      ) : (
        <Box sx={{ display: "flex", flexDirection: "column", gap: 0.5 }}>
          <Typography sx={{ color: "text.secondary" }}>
            Signed in{label ? ` as ${label}` : ""}{me?.provider ? ` (${me.provider})` : ""}.
          </Typography>
          {me?.roles && me.roles.length > 0 ? (
            <Typography sx={{ color: "text.secondary" }}>
              Roles: {me.roles.join(", ")}
            </Typography>
          ) : null}
          {me?.googleLink?.linked ? (
            <Typography sx={{ color: "text.secondary" }}>
              Google linked{me.googleLink.email ? `: ${me.googleLink.email}` : "."}
            </Typography>
          ) : null}

          <Box sx={{ mt: 1 }}>
            <Button
              variant="outlined"
              onClick={() => {
                clearAuthToken();
                setMe(null);
                toast("Signed out.", "info");
              }}
            >
              Sign out
            </Button>
          </Box>
        </Box>
      )}
    </Paper>
  );
}
