import type { Player } from "@/server/db/schema";
import React from "react";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { ShieldQuestion, UserX } from "lucide-react";
import { api } from "@/trpc/react";
import { Button } from "@/components/ui/button";

function KickPlayerDialog({
  player,
  lobbyId,
}: {
  player: Pick<Player, "id" | "displayName" | "avatar">;
  lobbyId: string;
}) {
  const kickPlayer = api.lobby.kick.useMutation();
  const handleAction = () => {
    kickPlayer.mutate({ lobbyId: lobbyId, playerId: player.id });
  };
  return (
    <AlertDialog>
      <AlertDialogTrigger className="flex items-center gap-1">
        <UserX className="group-focus:text-destructive size-4 text-white" />
        Kick Player
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle className="flex items-center gap-1">
            Are you absolutely sure to
            <span className="text-destructive font-bold">
              kick {player.displayName}
            </span>
            <ShieldQuestion className="text-destructive size-4" />
          </AlertDialogTitle>
          <AlertDialogDescription>
            This action cannot be undone. This will remove {player.displayName}
            from the lobby.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <Button
            asChild
            variant={"destructive"}
            disabled={kickPlayer.isPending}
          >
            <AlertDialogAction onClick={handleAction}>
              {kickPlayer.isPending ? "Kicking..." : "Kick Player"}
            </AlertDialogAction>
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

export default KickPlayerDialog;
