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 Swal from "sweetalert2";

import { Link, useRouter } from "@/i18n/routing";
import withReactContent from "sweetalert2-react-content";
import { close, error, loading } from "@/config/swal";
import { deleteMediaBlastAccount } from "@/service/broadcast/broadcast";
import { useToast } from "@/components/ui/use-toast";

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

  {
    accessorKey: "accountName",
    header: "Nama",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("accountName")}</span>
    ),
  },
  {
    accessorKey: "accountType",
    header: "Tipe Akun",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("accountType")}</span>
    ),
  },
  {
    accessorKey: "accountCategory",
    header: "Kategory",
    cell: ({ row }) => (
      <span className="uppercase">{row.getValue("accountCategory")}</span>
    ),
  },
  {
    accessorKey: "emailAddress",
    header: "Email",
    cell: ({ row }) => (
      <span className="normal-case">{row.getValue("emailAddress")}</span>
    ),
  },
  {
    accessorKey: "whatsappNumber",
    header: "Whatsapp",
    cell: ({ row }) => <span>{row.getValue("whatsappNumber")}</span>,
  },
  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    enableHiding: false,
    cell: ({ row }) => {
      const MySwal = withReactContent(Swal);
      const { toast } = useToast();
      const router = useRouter();
      const handleDelete = (id: string) => {
        MySwal.fire({
          title: "Apakah anda ingin menghapus data?",
          showCancelButton: true,
          confirmButtonColor: "#dc3545",
          confirmButtonText: "Iya",
          cancelButtonText: "Tidak",
        }).then((result) => {
          if (result.isConfirmed) {
            doDeleteAccount(id);
          }
        });
      };

      async function succes() {
        toast({
          title: "Sukses",
          description: "Berhasil Delete",
        });
        window.location.reload();
      }

      async function doDeleteAccount(id: string) {
        loading();
        const response = await deleteMediaBlastAccount(id);
        close();
        if (response?.error) {
          error(response.message);
          return false;
        }
        succes();
      }
      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">
              <Link
                href={`/admin/broadcast/campaign-list/account-list/edit/${row.original.id}`}
              >
                Edit
              </Link>
            </DropdownMenuItem>
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <a onClick={() => handleDelete(row.original.id)}>Delete</a>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      );
    },
  },
];

export default columns;
