import { Link } from "react-router-dom";
import { FaCreditCard, FaFileInvoiceDollar } from "react-icons/fa";
import { GiPayMoney, GiPlayerTime } from "react-icons/gi";
import { BankFilled, ExportOutlined, FieldTimeOutlined } from "@ant-design/icons";
import LockWrapper from "../../components/lock-wrapper/lock-wrapper.component.jsx";
import { HasFeatureAccess } from "../../components/feature-wrapper/feature-wrapper.component";

// --- Menu Item Builders ---
const buildAccountingChildren = ({
  t,
  bodyshop,
  currentUser,
  setBillEnterContext,
  setPaymentContext,
  setCardPaymentContext,
  setTimeTicketContext,
  ImEXPay,
  DmsAp,
  Simple_Inventory
}) => [
  {
    key: "bills",
    id: "header-accounting-bills",
    icon: <FaFileInvoiceDollar />,
    label: (
      <Link to="/manage/bills">
        <LockWrapper featureName="bills" bodyshop={bodyshop}>
          {t("menus.header.bills")}
        </LockWrapper>
      </Link>
    )
  },
  {
    key: "enterbills",
    id: "header-accounting-enterbills",
    icon: <GiPayMoney />,
    label: (
      <LockWrapper featureName="bills" bodyshop={bodyshop}>
        {t("menus.header.enterbills")}
      </LockWrapper>
    ),
    onClick: () =>
      HasFeatureAccess({ featureName: "bills", bodyshop }) && setBillEnterContext({ actions: {}, context: {} })
  },
  ...(Simple_Inventory.treatment === "on"
    ? [
        { type: "divider" },
        {
          key: "inventory",
          id: "header-accounting-inventory",
          icon: <FaFileInvoiceDollar />,
          label: <Link to="/manage/inventory">{t("menus.header.inventory")}</Link>
        }
      ]
    : []),
  { type: "divider" },
  {
    key: "allpayments",
    id: "header-accounting-allpayments",
    icon: <BankFilled />,
    label: <Link to="/manage/payments">{t("menus.header.allpayments")}</Link>
  },
  {
    key: "enterpayments",
    id: "header-accounting-enterpayments",
    icon: <FaCreditCard />,
    label: t("menus.header.enterpayment"),
    onClick: () => setPaymentContext({ actions: {}, context: null })
  },
  ...(ImEXPay.treatment === "on"
    ? [
        {
          key: "entercardpayments",
          id: "header-accounting-entercardpayments",
          icon: <FaCreditCard />,
          label: t("menus.header.entercardpayment"),
          onClick: () => setCardPaymentContext({ actions: {}, context: {} })
        }
      ]
    : []),
  { type: "divider" },
  {
    key: "timetickets",
    id: "header-accounting-timetickets",
    icon: <FieldTimeOutlined />,
    label: (
      <Link to="/manage/timetickets">
        <LockWrapper featureName="timetickets" bodyshop={bodyshop}>
          {t("menus.header.timetickets")}
        </LockWrapper>
      </Link>
    )
  },
  ...(bodyshop?.md_tasks_presets?.use_approvals
    ? [
        {
          key: "ttapprovals",
          id: "header-accounting-ttapprovals",
          icon: <FieldTimeOutlined />,
          label: <Link to="/manage/ttapprovals">{t("menus.header.ttapprovals")}</Link>
        }
      ]
    : []),
  {
    key: "entertimetickets",
    id: "header-accounting-entertimetickets",
    icon: <GiPlayerTime />,
    label: (
      <LockWrapper featureName="timetickets" bodyshop={bodyshop}>
        {t("menus.header.entertimeticket")}
      </LockWrapper>
    ),
    onClick: () =>
      HasFeatureAccess({ featureName: "timetickets", bodyshop }) &&
      setTimeTicketContext({
        actions: {},
        context: {
          created_by: currentUser.displayName ? `${currentUser.email} | ${currentUser.displayName}` : currentUser.email
        }
      })
  },
  { type: "divider" },
  {
    key: "accountingexport",
    id: "header-accounting-export",
    icon: <ExportOutlined />,
    label: (
      <LockWrapper featureName="export" bodyshop={bodyshop}>
        {t("menus.header.export")}
      </LockWrapper>
    ),
    children: [
      {
        key: "receivables",
        id: "header-accounting-receivables",
        label: (
          <Link to="/manage/accounting/receivables">
            <LockWrapper featureName="export" bodyshop={bodyshop}>
              {t("menus.header.accounting-receivables")}
            </LockWrapper>
          </Link>
        )
      },
      ...(!((bodyshop && bodyshop.cdk_dealerid) || (bodyshop && bodyshop.pbs_serialnumber)) || DmsAp.treatment === "on"
        ? [
            {
              key: "payables",
              id: "header-accounting-payables",
              label: (
                <Link to="/manage/accounting/payables">
                  <LockWrapper featureName="export" bodyshop={bodyshop}>
                    {t("menus.header.accounting-payables")}
                  </LockWrapper>
                </Link>
              )
            }
          ]
        : []),
      ...(!((bodyshop && bodyshop.cdk_dealerid) || (bodyshop && bodyshop.pbs_serialnumber))
        ? [
            {
              key: "payments",
              id: "header-accounting-payments",
              label: (
                <Link to="/manage/accounting/payments">
                  <LockWrapper featureName="export" bodyshop={bodyshop}>
                    {t("menus.header.accounting-payments")}
                  </LockWrapper>
                </Link>
              )
            }
          ]
        : []),
      { type: "divider" },
      {
        key: "exportlogs",
        id: "header-accounting-exportlogs",
        label: (
          <Link to="/manage/accounting/exportlogs">
            <LockWrapper featureName="export" bodyshop={bodyshop}>
              {t("menus.header.export-logs")}
            </LockWrapper>
          </Link>
        )
      }
    ]
  }
];

export default buildAccountingChildren;
