import { supabase } from "@/lib/supabase";
import { Session } from "@supabase/supabase-js";
import { Redirect, Tabs } from "expo-router";
import { useEffect, useState } from "react";

export default function TabLayout() {
  const [session, SetSession] = useState<Session | null>(null)
  const [loading, SetLoading] = useState(true);

  useEffect(() => {
    const loadSession = async () => {
      const { data } = await supabase.auth.getSession();
      SetSession(data.session ?? null);
      SetLoading(false);
    }
    loadSession();

    const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
      SetSession(newSession);
      SetLoading(false);
    });

    return () => sub.subscription.unsubscribe();
  }, []);

  if (loading) {
    return null;
  }

  if (!session) {
    return <Redirect href="/createUser" />;
  }

  return (
    <Tabs>
      <Tabs.Screen name="index" options={{title: "Index"}} />
      <Tabs.Screen name="tasks" options={{title: "Tasks"}} />
      <Tabs.Screen name="assignments" options={{title: "Assignments"}} />
      <Tabs.Screen name="subjects" options={{title: "Subjects"}} />
      <Tabs.Screen name="timer" options={{title: "Timer"}} />

      <Tabs.Screen name="subject/createSubject" options={{ href: null }} />
      <Tabs.Screen name="subject/editSubject" options={{ href: null }} />
      <Tabs.Screen name="subject/viewDetailsSubject" options={{ href: null }} />

      <Tabs.Screen name="assignment/createAssignment" options={{ href: null }} />
      <Tabs.Screen name="assignment/editAssignment" options={{ href: null }} />
      <Tabs.Screen name="assignment/viewDetailsAssignment" options={{ href: null }} />

      <Tabs.Screen name="task/createTask" options={{ href: null }} />
      <Tabs.Screen name="task/editTask" options={{ href: null }} />
      <Tabs.Screen name="task/viewDetailsTask" options={{ href: null }} />
    </Tabs>
  );
}