import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
import { Database } from "@/types/supabase";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { CreateTask } from "@/components/create-task";
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetHeader,
  SheetTitle,
  SheetTrigger,
} from "@/components/ui/sheet";
import { Checkbox } from "@/components/ui/checkbox";

import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import Chat from "@/components/chat";
// import GenerateTasks from "./generate-tasks";

async function getSession(supabase: any) {
  const {
    data: { session },
  } = await supabase.auth.getSession();
  return session;
}

async function fetchTasks(projectID: string) {
  const supabase = createServerComponentClient<Database>({ cookies });
  const session = await getSession(supabase);

  if (!session) redirect("/auth");

  const tasks = await prisma.task.findMany({
    where: {
      project_id: BigInt(projectID),
    },
    include: {
      feature: true,
    },
  });

  if (!tasks) return undefined;

  const res = tasks.map((task) => ({
    ...task,
    id: String(task.id),
    project_id: String(task.project_id),
    feature_id: String(task.feature_id),
  }));

  return res;
}

export async function TaskList({
  workspaceID,
  projectID,
  project_name,
  project_description,
  tech_stack,
}: {
  workspaceID: string;
  projectID: string;
  project_name: string;
  project_description: string;
  tech_stack: string[];
}) {
  const tasks = await fetchTasks(projectID);
  console.log(project_name);

  return (
    <div>
      <h1 className="font-bold">Task List</h1>
      {tasks?.length != 0 ? (
        tasks?.map((task, i) => (
          <div key={i}>
            <Sheet>
              <SheetTrigger>{task.name}</SheetTrigger>
              <SheetContent className="w-[800px]">
                <SheetHeader>
                  <SheetTitle>{task.name}</SheetTitle>
                  <SheetDescription className="flex flex-col space-y-4">
                    <p className="truncate">{task.description}</p>
                    <Chat
                      projectInfo={{
                        name: project_name,
                        description: project_description,
                        stack: tech_stack,
                      }}
                      featureInfo={
                        task.feature
                          ? task.feature
                          : { name: "", description: "" }
                      }
                      taskInfo={{
                        task_name: task.name,
                        task_description: task.description,
                        task_id: task.id,
                      }}
                    />
                  </SheetDescription>
                </SheetHeader>
              </SheetContent>
            </Sheet>
          </div>
        ))
      ) : (
        <p>No tasks</p>
      )}
      <CreateTask workspaceID={workspaceID} projectID={projectID} />
      {/* <GenerateTasks stack={["Next.js", "Supabase"]} /> */}
    </div>
  );
}
