"use client";
import Link from "next/link";
import { useAuth } from "react-oidc-context";
import { useState, useEffect, use } from "react";
import { useRouter, usePathname } from "next/navigation";
import { api, Session } from "muzak/data/fetcher";
import { getUser } from "muzak/data/fetcher";
import { useSession } from "muzak/contexts/SessionContext";
import getSessionId from "muzak/data/session";

export default function SessionChecker() {
  const auth = useAuth();
  const router = useRouter();

  const [isSession, setIsSession] = useState(false);
  const [link, setLink] = useState("/lobby");
  const { sessionId, setSessionId } = useSession();

  // async function getSessions() {
  //   if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
  //     const { data: sessions } = await api?.sessions?.list({});
  //     console.log(sessions);
  //     if (sessions?.length > 0) {
  //       setIsSession(true);
  //       const user = getUser();
  //       setSessionId(sessions[0].session_id);
  //       if (sessions[0].host === user?.profile?.email) {
  //         setLink("/lobby");
  //       } else {
  //         setLink("/session");
  //       }
  //     } else {
  //       setIsSession(false);
  //     }
  //   }
  // }

  useEffect(() => {
    //getSessionId();
    console.log("hheufheufh");
  }, [auth]);

  if (sessionId) {
    return (
      <Link
        href={link}
        className="bold text-l absolute top-2 left-2 rounded-[50%] bg-red-500 p-3 font-bold text-white"
      >
        Session available!
      </Link>
    );
  }
  return <> </>;
}
