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

import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";

import { Link, useRouter } from "@/i18n/routing";
import { error } from "@/config/swal";
import { deleteCategory, deleteDataFAQ } from "@/service/settings/settings";
import { useToast } from "@/components/ui/use-toast";

import {
  Menubar,
  MenubarContent,
  MenubarMenu,
  MenubarTrigger,
} from "@/components/ui/menubar";
import { htmlToString } from "@/utils/globals";
import EditSpvFAQModal from "./edit";

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

  {
    accessorKey: "question",
    header: "Pertanyaan",
    cell: ({ row }) => (
      <span className="normal-case">
        {htmlToString(row.getValue("question"))}
      </span>
    ),
  },
  {
    accessorKey: "answer",
    header: "Answer",
    cell: ({ row }) => (
      <span className="normal-case">
        {htmlToString(row.getValue("answer"))}
      </span>
    ),
  },

  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    enableHiding: false,
    cell: ({ row }) => {
      const router = useRouter();
      const { toast } = useToast();

      const faqDelete = async (id: string) => {
        const response = await deleteDataFAQ(id);
        console.log(response);
        if (response?.error) {
          error(response.message);
          return false;
        }
        toast({
          title: "Sukses",
          description: "Berhasil Delete",
        });
        router.push("/supervisor/faq?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">
              <EditSpvFAQModal id={row.original.id} isDetail={true} />

              <EditSpvFAQModal id={row.original.id} isDetail={false} />

              <a
                onClick={() => faqDelete(row.original.id)}
                className="hover:underline cursor-pointer hover:text-destructive"
              >
                Delete
              </a>
            </MenubarContent>
          </MenubarMenu>
        </Menubar>
      );
    },
  },
];

export default columns;
