import { defineStore } from "pinia";
import { computed } from "vue";
import { permissionStore } from "src/stores/permission";
import { userStore } from "src/stores/user";

export const navigationStore = defineStore("navigation", () => {
  const navigationStructure = Object.freeze([
    {
      type: "single",
      title: "Dashboard",
      name: "DashboardPage",
      icon: "mdi-poll",
      disable: false,
      permission: false,
      permissionScope: "dashboard",
    },
    {
      type: "single",
      title: "Franqueados",
      name: "FranchiseePage",
      icon: "mdi-home-variant-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_franchisee",
    },
    {
      type: "single",
      title: "Pacotes",
      name: "PackagesPage",
      icon: "mdi-package-variant-closed",
      disable: false,
      permission: false,
      permissionScope: "franchisor_packages",
    },
    {
      type: "single",
      title: "Produtos",
      name: "ProductsPage",
      icon: "mdi-cart-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_products",
    },
    {
      type: "single",
      title: "TBR",
      name: "TbrPage",
      icon: "mdi-database-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_tbr",
    },
    {
      type: "expansive",
      title: "Financeiro",
      name: "FinancialPage",
      icon: "mdi-cash-multiple",
      disable: false,
      permission: false,
      permissionScope: "franchisor_financial",
      childrens: [
        {
          title: "Tesouraria",
          name: "TreasuryPage",
          icon: "mdi-bank-outline",
          permission: true,
        },
        {
          title: "Contas a Pagar",
          name: "AccountsPayablePage",
          icon: "mdi-cash-minus",
          permission: true,
        },
        {
          title: "Contas a Receber",
          name: "AccountsReceivablePage",
          icon: "mdi-cash-plus",
          permission: true,
        },
        {
          title: "Plano de Contas",
          name: "ChartOfAccountsPage",
          icon: "mdi-format-list-bulleted",
          permission: true,
        },
        {
          title: "Emissão de Notas",
          name: "InvoiceIssuancePage",
          icon: "mdi-file-document-outline",
          permission: true,
        },
        {
          title: "Integração Asaas",
          name: "AsaasIntegrationPage",
          icon: "mdi-api",
          permission: true,
        },
      ],
    },
    {
      type: "single",
      title: "Suporte",
      name: "SupportPage",
      icon: "mdi-headset",
      disable: false,
      permission: false,
      permissionScope: "franchisor_support",
    },
    {
      type: "single",
      title: "Atividades",
      name: "KanbanPage",
      icon: "mdi-clipboard-list-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_activities",
    },
    {
      type: "single",
      title: "Cadastros",
      name: "CadastrosPage",
      icon: "mdi-account-multiple-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_registrations",
    },
    {
      type: "single",
      title: "Permissões",
      name: "PermissionsPage",
      icon: "mdi-shield-key-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisor_registrations",
      adminOnly: true,
    },
  ]);

  const getNavigationAccess = () => {
    const { getAccess } = permissionStore();
    const { isAdmin } = userStore();
    return navigationStructure
      .filter((menu) => !menu.adminOnly || isAdmin)
      .map((menu) => {
        if (menu.type === "expansive") {
          if (getAccess(menu.permissionScope, "menu")) menu.permission = true;
          menu.childrens = menu.childrens.filter((children) => {
            if (!children?.permissionScope) return true;
            children.permission = getAccess(children.permissionScope, "menu");
            return children.permission;
          });
          return menu.childrens.length > 0 ? menu : null;
        } else {
          if (!menu?.permissionScope) return menu;
          menu.permission = getAccess(menu.permissionScope, "menu");
          return menu;
        }
      })
      .filter((menu) => menu !== null);
  };

  const navigationItems = computed(() => getNavigationAccess());

  return {
    navigationItems,
  };
});
