import React, { useState } from "react";
import { useDeleteExpense, useExpenseList } from "../../hooks/useExpense";
import Avatar from "../common/Avatar";
import { useExpenseContext } from "../../pages/Expense/ExpensePage";
import { formatDate, formatUTCToLocalTime } from "../../utils/dateUtils";
import Pagination from "../common/Pagination";
import {
  APPROVE_EXPENSE,
  EXPENSE_DRAFT,
  EXPENSE_REJECTEDBY,
} from "../../utils/constants";
import { getColorNameFromHex, useDebounce } from "../../utils/appUtils";
import { ExpenseTableSkeleton } from "./ExpenseSkeleton";
import ConfirmModal from "../common/ConfirmModal";
import { useHasUserPermission } from "../../hooks/useHasUserPermission";
import { useSelector } from "react-redux";

const ExpenseList = ({ filters, groupBy = "transactionDate", searchText }) => {
  const [deletingId, setDeletingId] = useState(null);
  const [IsDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  const { setViewExpense, setManageExpenseModal } = useExpenseContext();
  const IsExpenseEditable = useHasUserPermission();
  const IsExpesneApprpve = useHasUserPermission(APPROVE_EXPENSE);
  const [currentPage, setCurrentPage] = useState(1);
  const pageSize = 20;
  const debouncedSearch = useDebounce(searchText, 500);

  const { mutate: DeleteExpense, isPending } = useDeleteExpense();
  const { data, isLoading, isError, isInitialLoading, error } = useExpenseList(
    pageSize,
    currentPage,
    filters,
    debouncedSearch
  );

  const SelfId = useSelector(
    (store) => store?.globalVariables?.loginUser?.employeeInfo?.id
  );

  const handleDelete = (id) => {
    setDeletingId(id);
    DeleteExpense(
      { id },
      {
        onSettled: () => {
          setDeletingId(null);
          setIsDeleteModalOpen(false);
        },
      }
    );
  };

  const paginate = (page) => {
    if (page >= 1 && page <= (data?.totalPages ?? 1)) {
      setCurrentPage(page);
    }
  };

  const groupByField = (items, field) => {
    return items.reduce((acc, item) => {
      let key;
      switch (field) {
        case "transactionDate":
          key = item.transactionDate?.split("T")[0];
          break;
        case "status":
          key = item.status?.displayName || "Unknown";
          break;
        case "submittedBy":
          key = `${item.createdBy?.firstName ?? ""} ${
            item.createdBy?.lastName ?? ""
          }`.trim();
          break;
        case "project":
          key = item.project?.name || "Unknown Project";
          break;
        case "paymentMode":
          key = item.paymentMode?.name || "Unknown Mode";
          break;
        case "expensesType":
          key = item.expensesType?.name || "Unknown Type";
          break;
        case "createdAt":
          key = item.createdAt?.split("T")[0] || "Unknown Type";
          break;
        default:
          key = "Others";
      }
      if (!acc[key]) acc[key] = [];
      acc[key].push(item);
      return acc;
    }, {});
  };

  const expenseColumns = [
    {
      key: "expensesType",
      label: "Expense Type",
      getValue: (e) => e.expensesType?.name || "N/A",
      align: "text-start",
    },
    {
      key: "paymentMode",
      label: "Payment Mode",
      getValue: (e) => e.paymentMode?.name || "N/A",
      align: "text-start",
    },
    {
      key: "Submitted By",
      label: "Submitted By",
      align: "text-start",
      getValue: (e) =>
        `${e.createdBy?.firstName ?? ""} ${e.createdBy?.lastName ?? ""}`.trim() ||
        "N/A",
      customRender: (e) => (
        <div className="d-flex align-items-center">
          <Avatar
            size="xs"
            classAvatar="m-0"
            firstName={e.createdBy?.firstName}
            lastName={e.createdBy?.lastName}
          />
          <span className="text-truncate">
            {`${e.createdBy?.firstName ?? ""} ${
              e.createdBy?.lastName ?? ""
            }`.trim() || "N/A"}
          </span>
        </div>
      ),
    },
    {
      key: "submitted",
      label: "Submitted",
      getValue: (e) => formatUTCToLocalTime(e?.createdAt),
      isAlwaysVisible: true,
    },
    {
      key: "amount",
      label: "Amount",
      getValue: (e) => (
        <>
          <i className="bx bx-rupee b-xs"></i> {e?.amount}
        </>
      ),
      isAlwaysVisible: true,
      align: "text-end",
    },
    {
      key: "status",
      label: "Status",
      align: "text-center",
      getValue: (e) => (
        <span
          className={`badge bg-label-${
            getColorNameFromHex(e?.status?.color) || "secondary"
          }`}
        >
          {e.status?.name || "Unknown"}
        </span>
      ),
    },
  ];

  if (isInitialLoading) return <ExpenseTableSkeleton />;
  if (isError) return <div>{error.message}</div>;

  const grouped = groupBy
    ? groupByField(data?.data ?? [], groupBy)
    : { All: data?.data ?? [] };
  const IsGroupedByDate = ["transactionDate", "createdAt"].includes(groupBy);
  const canEditExpense = (expense) => {
    return (
      (expense.status.id === EXPENSE_DRAFT ||
        EXPENSE_REJECTEDBY.includes(expense.status.id)) &&
      expense.createdBy?.id === SelfId
    );
  };

  const canDetetExpense = (expense) => {
    return (
      expense.status.id === EXPENSE_DRAFT && expense.createdBy.id === SelfId
    );
  };

  return (
    <>
      {IsDeleteModalOpen && (
        <div
          className={`modal fade show`}
          tabIndex="-1"
          role="dialog"
          style={{
            display: "block",
            backgroundColor: "rgba(0,0,0,0.5)",
          }}
          aria-hidden="false"
        >
          <ConfirmModal
            type="delete"
            header="Delete Expense"
            message="Are you sure you want delete?"
            onSubmit={handleDelete}
            onClose={() => setIsDeleteModalOpen(false)}
            loading={isPending}
            paramData={deletingId}
          />
        </div>
      )}

      <div className="card px-0 px-sm-4">
        <div
          className="card-datatable table-responsive "
          id="horizontal-example"
        >
          <div className="dataTables_wrapper no-footer px-2 ">
            <table className="table border-top dataTable text-nowrap">
              <thead>
                <tr>
                  {expenseColumns.map(
                    (col) =>
                      (col.isAlwaysVisible || groupBy !== col.key) && (
                        <th
                          key={col.key}
                          className={`sorting d-table-cell`}
                          aria-sort="descending"
                        >
                          <div className={`${col.align}`}>{col.label}</div>
                        </th>
                      )
                  )}
                  <th className="sticky-action-column bg-white text-center">
                    Action
                  </th>
                </tr>
              </thead>
              <tbody>
                {Object.keys(grouped).length > 0 ? (
                  Object.entries(grouped).map(([group, expenses]) => (
                    <React.Fragment key={group}>
                      <tr className="tr-group text-dark">
                        <td colSpan={8} className="text-start">
                          <strong>
                            {IsGroupedByDate
                              ? formatUTCToLocalTime(group)
                              : group}
                          </strong>
                        </td>
                      </tr>
                      {expenses.map((expense) => (
                        <tr key={expense.id}>
                          {expenseColumns.map(
                            (col) =>
                              (col.isAlwaysVisible || groupBy !== col.key) && (
                                <td
                                  key={col.key}
                                  className={`d-table-cell ${col.align ?? ""}`}
                                >
                                  {col.customRender
                                    ? col.customRender(expense)
                                    : col.getValue(expense)}
                                </td>
                              )
                          )}
                          <td className="sticky-action-column bg-white">
                            <div className="d-flex justify-content-center gap-2">
                              <i
                                className="bx bx-show text-primary cursor-pointer"
                                onClick={() =>
                                  setViewExpense({
                                    expenseId: expense.id,
                                    view: true,
                                  })
                                }
                              ></i>
                              {canEditExpense(expense) && (
                                <i
                                  className="bx bx-edit text-secondary cursor-pointer"
                                  onClick={() =>
                                    setManageExpenseModal({
                                      IsOpen: true,
                                      expenseId: expense.id,
                                    })
                                  }
                                ></i>
                              )}

                              {canDetetExpense(expense) && (
                                <i
                                  className="bx bx-trash text-danger cursor-pointer"
                                  onClick={() => {
                                    setIsDeleteModalOpen(true);
                                    setDeletingId(expense.id);
                                  }}
                                ></i>
                              )}
                            </div>
                          </td>
                        </tr>
                      ))}
                    </React.Fragment>
                  ))
                ) : (
                  <tr>
                    <td colSpan={8} className="text-center py-4">
                      No Expense Found
                    </td>
                  </tr>
                )}
              </tbody>
            </table>
            {data?.data?.length > 0 && (
              <Pagination
                currentPage={currentPage}
                totalPages={data.totalPages}
                onPageChange={paginate}
              />
            )}
          </div>
        </div>
      </div>
    </>
  );
};

export default ExpenseList;
