import { Eye, Lock1 } from "iconsax-react";
import type { ColumnType } from "@/components/types/TableTypes";
import type { CashShift } from "../types/Types";
import {
  formatFaNumber,
  formatOptionalDate,
  formatPrice,
} from "@/helpers/func";
import Status from "@/components/Status";
import { CashShiftStatus } from "../enum/Enum";
import type { TFunction } from "i18next";

interface GetCashShiftTableColumnsParams {
  t: TFunction;
  onView: (shift: CashShift) => void;
  onClose?: (shift: CashShift) => void;
}

const getAdminName = (shift: CashShift) => {
  const name = [shift.admin?.firstName, shift.admin?.lastName]
    .filter(Boolean)
    .join(" ");
  return name || shift.admin?.phone || "-";
};

export const getCashShiftTableColumns = ({
  t,
  onView,
  onClose,
}: GetCashShiftTableColumnsParams): ColumnType<CashShift>[] => [
  {
    key: "openedAt",
    title: t("cash_shift.opened_at"),
    render: (item) =>
      formatOptionalDate(item.openedAt, {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
      }),
  },
  {
    key: "closedAt",
    title: t("cash_shift.closed_at"),
    render: (item) =>
      formatOptionalDate(item.closedAt, {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
      }),
  },
  {
    key: "admin",
    title: t("cash_shift.operator"),
    render: (item) => getAdminName(item),
  },
  {
    key: "openingAmount",
    title: t("cash_shift.opening_amount"),
    render: (item) => formatPrice(item.openingAmount),
  },
  {
    key: "expectedAmount",
    title: t("cash_shift.expected_amount"),
    render: (item) =>
      item.expectedAmount !== null ? formatPrice(item.expectedAmount) : "-",
  },
  {
    key: "differenceAmount",
    title: t("cash_shift.difference_amount"),
    render: (item) => {
      if (item.differenceAmount === null) return "-";
      const value = formatPrice(item.differenceAmount);
      if (item.differenceAmount > 0) {
        return <span className="text-green-600">{value}</span>;
      }
      if (item.differenceAmount < 0) {
        return <span className="text-red-600">{value}</span>;
      }
      return value;
    },
  },
  {
    key: "ordersCount",
    title: t("cash_shift.orders_count"),
    render: (item) => formatFaNumber(item.ordersCount),
  },
  {
    key: "status",
    title: t("cash_shift.status_label"),
    render: (item) => (
      <Status
        variant={item.status === CashShiftStatus.OPEN ? "warning" : "success"}
        label={t(`cash_shift.status_values.${item.status}`)}
      />
    ),
  },
  {
    key: "actions",
    title: "",
    render: (item) => (
      <div className="flex items-center gap-2">
        <button
          onClick={() => onView(item)}
          className="cursor-pointer hover:opacity-70 transition-opacity"
          title={t("cash_shift.view_details")}
        >
          <Eye size={20} color="#8C90A3" />
        </button>
        {item.status === CashShiftStatus.OPEN && onClose && (
          <button
            onClick={() => onClose(item)}
            className="cursor-pointer hover:opacity-70 transition-opacity"
            title={t("cash_shift.close_shift")}
          >
            <Lock1 size={20} color="#8C90A3" />
          </button>
        )}
      </div>
    ),
  },
];
