import { ServiceConfig } from "../lib/service";
// import { generateSelectOneAction } from "../lib/actions";

const getQRCode = async (token: string) => {
  const url = `/api/whatsapp/bots/${token}`;
  const result = await fetch(url, { cache: "no-store" });
  const { qr } = await result.json();

  return { qr, kind: "data" };
};

export const whatsappConfig: ServiceConfig = {
  entity: "whatsapp",
  table: "WhatsappBot",
  displayName: "WhatsApp Connection",
  createFields: [
    {
      name: "name",
      label: "Name",
      required: true,
      size: 12,
    },
    {
      name: "description",
      label: "Description",
      size: 12,
      lines: 3,
    },
    {
      name: "phoneNumber",
      label: "Phone Number",
      required: true,
    },
    {
      name: "token",
      label: "Token",
      hidden: true,
      required: true,
      autogenerated: "token",
    },
  ],
  updateFields: [
    { name: "name", label: "Name", required: true, size: 12 },
    {
      name: "description",
      label: "Description",
      size: 12,
    },
    {
      name: "phoneNumber",
      label: "Phone Number",
      required: true,
    },
  ],
  displayFields: [
    { name: "name", label: "Name", required: true, size: 12 },
    {
      name: "description",
      label: "Description",
      size: 12,
    },
    {
      name: "phoneNumber",
      label: "Phone Number",
    },
    {
      name: "token",
      label: "Token",
      copyable: true,
    },
    {
      name: "qrcode",
      label: "QR Code",
      kind: "qrcode",
      size: 4,
      getQRCode,
      helperText: "Go to link devices in the app, then scan the code",
      refreshInterval: 15,
    },
  ],
  listColumns: [
    {
      field: "name",
      headerName: "Name",
      flex: 1,
    },
    {
      field: "phoneNumber",
      headerName: "Phone Number",
      flex: 1,
    },
    {
      field: "description",
      headerName: "Description",
      flex: 2,
    },
    {
      field: "updatedAt",
      headerName: "Updated At",
      valueGetter: (value: any) => new Date(value).toLocaleString(),
      flex: 1,
    },
  ],
};
