import * as React from "react";
import { ColumnDef } from "@tanstack/react-table";

import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react";
import { cn } from "@/lib/utils";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
  DropdownMenuItem,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";

import { Link, useRouter } from "@/i18n/routing";
import StatusToogle from "./status";
import { error } from "@/config/swal";
import { deleteCategory } from "@/service/settings/settings";
import { useToast } from "@/components/ui/use-toast";
import EditCategoryModal from "./edit";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import {
  Menubar,
  MenubarContent,
  MenubarMenu,
  MenubarTrigger,
} from "@/components/ui/menubar";

const columns: ColumnDef<any>[] = [
  {
    accessorKey: "no",
    header: "No",
    cell: ({ row }) => <span>{row.getValue("no")}</span>,
  },

  {
    accessorKey: "name",
    header: "Nama Kategori",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("name")}</span>
    ),
  },
  {
    accessorKey: "contentType",
    header: "Tipe Konten",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("contentType")}</span>
    ),
  },
  {
    accessorKey: "isInternational",
    header: "Wilayah",
    cell: ({ row }) => (
      <span className="normal-case">
        {row.getValue("isInternational") ? "INT" : "ID"}
      </span>
    ),
  },
  {
    accessorKey: "status",
    header: "Status",
    cell: ({ row }) => (
      <span className="normal-case">
        <StatusToogle id={row.original.id} initValue={row.original.isPublish} />
      </span>
    ),
  },
  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    enableHiding: false,
    cell: ({ row }) => {
      const router = useRouter();
      const { toast } = useToast();

      const categoryDelete = async (id: number) => {
        const response = await deleteCategory(id);
        console.log(response);
        if (response?.error) {
          error(response.message);
          return false;
        }
        toast({
          title: "Sukses",
          description: "Berhasil Delete",
        });
        router.push("/admin/settings/category?dataChange=true");
      };
      return (
        <Menubar className="border-none">
          <MenubarMenu>
            <MenubarTrigger>
              <Button
                size="icon"
                className="bg-transparent ring-offset-transparent hover:bg-transparent hover:ring-0 hover:ring-transparent"
              >
                <MoreVertical className="h-4 w-4 text-default-800" />
              </Button>
            </MenubarTrigger>
            <MenubarContent className="flex flex-col gap-2 justify-center items-start p-4">
              <EditCategoryModal
                id={row.original.id}
                isDetail={true}
                thumbnailLink={row.original.thumbnailLink}
              />
              <EditCategoryModal
                id={row.original.id}
                thumbnailLink={row.original.thumbnailLink}
              />
              <a
                onClick={() => categoryDelete(row.original.id)}
                className="hover:underline cursor-pointer hover:text-destructive"
              >
                Delete
              </a>
            </MenubarContent>
          </MenubarMenu>
        </Menubar>
      );
    },
  },
];

export default columns;
