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

export const navigationStore = defineStore("navigation", () => {
  const navigationStructure = Object.freeze([
    {
      type: "single",
      title: "ui.navigation.dashboard",
      name: "DashboardPage",
      icon: "mdi-poll",
      disable: false,
      permission: false,
      permissionScope: "franchisee_dashboard",
    },
    {
      type: "single",
      title: "Alunos",
      name: "StudentPage",
      icon: "mdi-account-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_students",
    },
    {
      type: "single",
      title: "Contratos",
      name: "ContractsPage",
      icon: "mdi-file-document-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_contracts",
    },
    {
      type: "single",
      title: "Agenda / Aulas",
      name: "ClassPage",
      icon: "mdi-calendar",
      disable: false,
      permission: false,
      permissionScope: "franchisee_classes",
    },
    {
      type: "expansive",
      title: "Financeiro",
      icon: "mdi-finance",
      disable: false,
      permission: false,
      permissionScope: "franchisee_financial",
      childrens: [
        {
          type: "single",
          title: "Visão Geral",
          name: "FinancialPage",
          icon: "mdi-view-dashboard-outline",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Tesouraria",
          name: "TreasuryPage",
          icon: "mdi-bank-outline",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Contas a Receber",
          name: "AccountsReceivablePage",
          icon: "mdi-cash-plus",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Contas a Pagar",
          name: "AccountsPayablePage",
          icon: "mdi-cash-minus",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Plano de Contas",
          name: "ChartOfAccountsPage",
          icon: "mdi-format-list-bulleted",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Emissão de Notas",
          name: "InvoiceIssuancePage",
          icon: "mdi-file-document-outline",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
        {
          type: "single",
          title: "Integração Asaas",
          name: "AsaasIntegrationPage",
          icon: "mdi-api",
          disable: false,
          permission: false,
          permissionScope: "franchisee_financial",
        },
      ],
    },
    {
      type: "single",
      title: "Pacotes",
      name: "PackagesPage",
      icon: "mdi-package-variant-closed",
      disable: false,
      permission: false,
      permissionScope: "franchisee_packages",
    },
    {
      type: "single",
      title: "Usuários",
      name: "UsersPage",
      icon: "mdi-account-multiple-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_users",
    },
    {
      type: "single",
      title: "Permissões",
      name: "PermissionsPage",
      icon: "mdi-shield-account-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_permissions",
    },
    {
      type: "single",
      title: "Unidade",
      name: "UnitDataPage",
      icon: "mdi-cog-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_unit",
    },
    {
      type: "single",
      title: "Suporte",
      name: "SupportPage",
      icon: "mdi-lifebuoy",
      disable: false,
      permission: false,
      permissionScope: "franchisee_support",
    },
    {
      type: "single",
      title: "Atividades",
      name: "KanbanPage",
      icon: "mdi-clipboard-list-outline",
      disable: false,
      permission: false,
      permissionScope: "franchisee_activities",
    },
  ]);

  const getNavigationAccess = () => {
    const { getAccess } = permissionStore();
    return navigationStructure
      .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,
  };
});
