import { DelegatePositionDto } from "@/record-management/dto/delegation/delegationDto";
import { ColumnDef } from "@tanstack/react-table";
import dayjs from "dayjs";
import { t } from "i18next";
import { renderStatus } from "@/record-management/utils/renderDetails";
import { DelegationRecordActions } from "./delegationColumnAction";
import { format } from "date-fns";
import i18n from "i18next";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/shared/common/ui/tooltip";

export const DelegationsColumns = (
  refetch: () => void,
): ColumnDef<DelegatePositionDto>[] => [
  {
    accessorKey: "delegatedPosition",
    header: () => t("delegation.position"),
    enableHiding: true, //Delegated Position
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const positionName = isAmharic
        ? row.original.position?.name?.am
        : row.original.position?.name?.en;
      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {positionName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{positionName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "delegatedEmployee",
    header: () => t("delegation.delegatedEmployee"), //Delegated Employee
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const employeeName = isAmharic
        ? row.original.employee?.name.am
        : row.original.employee?.name.en;

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {employeeName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{employeeName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    }, // Replace with name if available
  },
  {
    accessorKey: "startDate",
    header: () => t("delegation.startDate"),
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const startDate = row.original.startDate;
      const amharicStartDate = row.original.amharicStartDate;

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

      const formattedDate = startDate
        ? new Date(startDate).toLocaleDateString("en-US", {
            month: "short",
            day: "numeric",
            year: "numeric",
            hour: "2-digit",
            minute: "2-digit",
          })
        : "-";

      return <div>{formattedDate}</div>;
    },
  },
  {
    accessorKey: "endDate",
    header: () => t("delegation.endDate"),
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const endDate = row.original.endDate;
      const amharicEndDate = row.original.amharicEndDate;

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

      const formattedDate = endDate
        ? new Date(endDate).toLocaleDateString("en-US", {
            month: "short",
            day: "numeric",
            year: "numeric",
            hour: "2-digit",
            minute: "2-digit",
          })
        : "-";

      return <div>{formattedDate}</div>;
    },
  },
  {
    accessorKey: "isCurrent",
    header: () => t("userIncoming.RecordStatus"),
    cell: ({ row }) => {
      const rowData = row.original;
      const isExpired = row.original.endDate
        ? dayjs(row.original.endDate).isBefore(dayjs())
        : false;
      const normalizedStatus =
        rowData.delegationLetter?.statusKey?.toLowerCase() ?? "";

      if (
        isExpired &&
        (normalizedStatus === "accepted" ||
          normalizedStatus === "approved" ||
          normalizedStatus === "adjustment")
      ) {
        return renderStatus("expired", "expired");
      }

      if (normalizedStatus) {
        return renderStatus(normalizedStatus, normalizedStatus);
      }

      return renderStatus("deactivated", "deactivated");
    },
    enableHiding: true,
  },
  {
    id: "Task Status",
    header: () => t("userIncoming.Status"),
    cell: ({ row }) => {
      const rowData = row.original;
      const normalizedStatus = rowData.status?.toLowerCase() ?? "";
      const isExpired = row.original.endDate
        ? dayjs(row.original.endDate).isBefore(dayjs())
        : false;

      if (normalizedStatus === "pending") {
        return isExpired
          ? renderStatus("expired", "expired")
          : renderStatus(normalizedStatus, normalizedStatus);
      }

      return renderStatus(normalizedStatus, normalizedStatus);
    },
    accessorKey: "TaskStatus",
    enableHiding: true,
    size: 120,
  },
  {
    id: "Actions",
    header: () => t("userRecord.Actions"),
    accessorKey: "Actions",
    enableHiding: true,
    cell: ({ row }) => {
      const rowData = row.original;

      return (
        <DelegationRecordActions
          rowData={rowData as unknown as DelegatePositionDto}
          refetchTable={refetch}
          type="delegator"
        />
      );
    },
  },
];
