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 { formatDateToIndonesian } from "@/utils/globals";
import { Link, useRouter } from "@/i18n/routing";

import { setBanner } from "@/service/settings/settings";
import { error } from "@/config/swal";
import { useToast } from "@/components/ui/use-toast";
import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";
import { deleteUser } from "@/service/management-user/management-user";
import { stringify } from "querystring";
const columns: ColumnDef<any>[] = [
  {
    accessorKey: "no",
    header: "No",
    cell: ({ row }) => <span>{row.getValue("no")}</span>,
  },

  {
    accessorKey: "fullname",
    header: "Nama",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("fullname")}</span>
    ),
  },
  {
    accessorKey: "username",
    header: "Username",
    cell: ({ row }) => (
      <span className="normal-case">
        {row.original?.userKeycloak?.username || ""}
      </span>
    ),
  },
  {
    accessorKey: "phoneNumber",
    header: "No. HP",
    cell: ({ row }) => <span>{row.getValue("phoneNumber")}</span>,
  },
  {
    accessorKey: "email",
    header: "Email",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("email")}</span>
    ),
  },
  {
    accessorKey: "level",
    header: "Level Pengguna",
    cell: ({ row }) => (
      <span className="normal-case">{row.original?.role?.name || ""}</span>
    ),
  },
  {
    accessorKey: "createdAt",
    header: "Tanggal Unggah",
    cell: ({ row }) => (
      <span>{formatDateToIndonesian(row.getValue("createdAt"))}</span>
    ),
  },

  {
    accessorKey: "isActive",
    header: "Status",
    cell: ({ row }) => (
      <span
        className={
          row.getValue("isActive") ? "text-success" : "text-destructive"
        }
      >
        {row.getValue("isActive") ? "Aktif" : "Belum Aktif"}
      </span>
    ),
  },

  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    enableHiding: false,
    cell: ({ row }) => {
      const { toast } = useToast();
      const MySwal = withReactContent(Swal);
      const router = useRouter();
      const doDelete = async (id: number) => {
        const response = await deleteUser(id);
        if (response?.error) {
          toast({
            title: stringify(response?.message),
            variant: "destructive",
          });
        }
        toast({
          title: "Success delete",
        });

        router.push("?dataChange=true");
      };

      const handleDelete = (id: number) => {
        MySwal.fire({
          title: "Apakah anda ingin menghapus data user?",
          showCancelButton: true,
          confirmButtonColor: "#dc3545",
          confirmButtonText: "Iya",
          cancelButtonText: "Tidak",
        }).then((result) => {
          if (result.isConfirmed) {
            doDelete(id);
          }
        });
      };
      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <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>
          </DropdownMenuTrigger>
          <DropdownMenuContent className="p-0" align="end">
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <a>Aktivasi</a>
            </DropdownMenuItem>
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <Link
                href={`/admin/management-user/external/detail/${row.original.id}`}
              >
                Detail
              </Link>
            </DropdownMenuItem>
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <Link
                href={`/admin/management-user/external/edit/${row.original.id}`}
              >
                Edit
              </Link>
            </DropdownMenuItem>
            <DropdownMenuItem
              color="red"
              className="p-2 border-b text-red-500 group focus:bg-red-500 focus:text-white rounded-none"
            >
              <a
                onClick={() => {
                  handleDelete(row.original.id);
                }}
              >
                Hapus
              </a>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      );
    },
  },
];

export default columns;
