"use client";

import {
  ArrowUpRight,
  FileText,
  Link,
  LoaderCircle,
  MoreHorizontal,
  RefreshCw,
  StarOff,
  Trash2,
} from "lucide-react";

import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
  SidebarGroup,
  SidebarGroupLabel,
  SidebarMenu,
  SidebarMenuAction,
  SidebarMenuButton,
  SidebarMenuItem,
  useSidebar,
} from "@/components/ui/sidebar";
import { createClient } from "@/utils/supabase/client";
import { toast } from "sonner";

export function NavDocuments({
  documents,
}: {
  documents: {
    id: string;
    disabled?: boolean;
    name: string;
    url: string;
    emoji?: string;
  }[];
}) {
  const { isMobile } = useSidebar();
  const supabase = createClient();

  return (
    <SidebarGroup className="group-data-[collapsible=icon]:hidden">
      <SidebarGroupLabel>Documents</SidebarGroupLabel>
      <SidebarMenu>
        {documents.map((item) => (
          <SidebarMenuItem key={item.id} aria-disabled={item.disabled}>
            <SidebarMenuButton asChild disabled={item.disabled}>
              <a href={item.url} title={item.name}>
                {item.disabled ? (
                  <LoaderCircle className="animate-spin text-muted-foreground" />
                ) : (
                  <span>{item.emoji ? item.emoji : <FileText />}</span>
                )}
                <span>{item.name}</span>
              </a>
            </SidebarMenuButton>
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <SidebarMenuAction showOnHover>
                  <MoreHorizontal />
                  <span className="sr-only">More</span>
                </SidebarMenuAction>
              </DropdownMenuTrigger>
              <DropdownMenuContent
                className="w-56 rounded-lg"
                side={isMobile ? "bottom" : "right"}
                align={isMobile ? "end" : "start"}
              >
                <DropdownMenuItem
                  onClick={async () => {
                    const data = new FormData();

                    const session = await supabase.auth.getSession();
                    if (!session.data.session) {
                      toast.error("You are not logged in");
                      return;
                    }

                    data.append("id", item.id);
                    data.append(
                      "access_token",
                      session.data.session.access_token
                    );
                    data.append(
                      "refresh_token",
                      session.data.session.refresh_token
                    );

                    toast.promise(
                      supabase.functions.invoke("process-document", {
                        body: data,
                      }),
                      {
                        loading: "Reprocessing document...",
                        success: "Document reprocessed successfully",
                        error: (err) => {
                          console.error("Error reprocessing document:", err);
                          return "Failed to reprocess document";
                        },
                      }
                    );
                  }}
                >
                  <RefreshCw className="text-muted-foreground" />
                  <span>Reprocess Document</span>
                </DropdownMenuItem>
                <DropdownMenuSeparator />
                <DropdownMenuItem>
                  <Trash2 className="text-muted-foreground" />
                  <span>Delete</span>
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          </SidebarMenuItem>
        ))}
        <SidebarMenuItem>
          <SidebarMenuButton className="text-sidebar-foreground/70">
            <MoreHorizontal />
            <span>More</span>
          </SidebarMenuButton>
        </SidebarMenuItem>
      </SidebarMenu>
    </SidebarGroup>
  );
}
