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

import { IncomingRecordActions } from "./incomingRecordActions";
import { CorrespondenceDto } from "@/record-management/dto/IncomingRecords/RecordOfficerIncoming";
import { t } from "i18next";
import i18n from "i18next";
import { renderStatus } from "@/record-management/utils/renderDetails";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/shared/common/ui/tooltip";

export const IncomingDraftRecordsColumns = (
  refetch: () => void,
  localizedName: (name?: { am?: string; en?: string }) => string,
  type: string,
  pageNo: number,
  openDropdownId?: string | null,
  onDropdownChange?: (id: string | null) => void,
  onAssignExternal?: (rowData: CorrespondenceDto) => void,
): ColumnDef<CorrespondenceDto>[] => [
  {
    accessorKey: "letterName",
    header: () => t("viewDetail.fileName1"),
    cell: ({ row }) => {
      const letterName = row.original?.letterName || "-";
      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {letterName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{letterName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "referenceNumber",
    header: () => t("userRecord.Reference Number"),
    cell: ({ row }) => {
      const referenceNumber = row.original?.referenceNumber || "-";

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help font-medium">
                {referenceNumber}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{referenceNumber}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "Letter_Number",
    header: () => t("userRecord.Letter Number"),
    cell: ({ row }) => {
      const letterNumber = row.original?.letterNumber || "-";

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {letterNumber}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{letterNumber}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "From",
    header: () => t("userIncoming.Sending Organization"),
    cell: ({ row }) => {
      const rowData = row?.original;
      const organizationName =
        localizedName(rowData?.metadata?.organizationName) || "-";

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {organizationName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{organizationName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "Subject",
    header: () => t("userRecord.Subject"),
    cell: ({ row }) => {
      const subject = row.original.content[0]?.subject || "-";

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {subject}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{subject}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    id: "Status",
    header: () => t("userRecord.Status"),
    cell: ({ row }) => {
      const status = row.original.nonSmartOfficeRecordStatus;
      return renderStatus(status, status);
    },
    enableHiding: true,
  },
  {
    accessorKey: "submittedDate",
    header: () => t("userRecord.Date"),
    cell: ({ row }) => {
      const record = row.original;

      const isAmharic = i18n.language === "am";
      const amharicDate = record.amharicCreatedAt;
      const submittedDate = record.createdAt;

      if (isAmharic && amharicDate) {
        return <div>{amharicDate}</div>;
      }

      const formattedDate = submittedDate
        ? new Date(submittedDate).toLocaleDateString(i18n.language, {
            year: "numeric",
            month: "short",
            day: "numeric",
            hour: "numeric",
            minute: "numeric",
          })
        : "-";

      return <div>{formattedDate}</div>;
    },
  },
  {
    id: "Actions",
    header: () => t("userRecord.Actions"),
    cell: ({ row }) => {
      const rowData = row.original;
      const dropdownId = rowData.id || "";

      return (
        <IncomingRecordActions
          shouldAddEditAction={
            type === "draft" || rowData.statusKey === "Draft"
          }
          rowData={rowData as CorrespondenceDto}
          refetchTable={refetch}
          type={type}
          pageIndex={pageNo}
          recordTypeKey="non_smart_user"
          onAssignExternal={(record) =>
            onAssignExternal?.(record as CorrespondenceDto)
          }
          isDropdownOpen={openDropdownId === dropdownId}
          onDropdownOpenChange={(open) => {
            onDropdownChange?.(open ? dropdownId : null);
          }}
          dropdownId={dropdownId}
        />
      );
    },
  },
];
