"use client";

import { Button } from "@/components/ui/button";
import { signIn, signOut, useSession } from "next-auth/react";
import Link from "next/link";

export function LoginButton() {
  const { data: session } = useSession();
  const callbackUrl =
    typeof window === "undefined"
      ? undefined
      : new URLSearchParams(document.location.search).get("callbackUrl") ??
        undefined;

  if (session) {
    return (
      <>
        <Link href="/panel">
          <Button variant="outline">continue</Button>
        </Link>
        <Button variant="destructive" onClick={() => signOut()}>
          log out
        </Button>
        <img
          src={`https://api.gifbox.me/file/posts/aYON6GqiqpwSpiZmAbJoOtw8tM2uYsEU.webp`}
          height={320}
        />
      </>
    );
  }

  return (
    <>
      <Button
        variant="outline"
        onClick={() =>
          signIn("authentik", {
            callbackUrl,
          })
        }
      >
        login with revolt sso
      </Button>
      <img
        src={`https://api.gifbox.me/file/posts/w7iUJfiyKA_zGkHN7Rr625WpaTHYgm4v.webp`}
        height={320}
      />
    </>
  );
}
