import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import contactPage from '@/views/contactPage.vue'
import AboutView from '../views/AboutView.vue'
import BanksView from '@/views/BanksView.vue'
import EntityView from '@/views/EntityView.vue'
import BankBranches from '@/views/BankBranches.vue'
import BankAccounts from '@/views/BankAccounts.vue'
import BillablesView from '@/views/BillablesView.vue'
import TaxesView from '@/views/TaxesView.vue'
import payeeEntityView from '@/views/payeeEntityView.vue'
import InvoiceTemplates from '@/views/InvoiceTemplates.vue'
import InvoiceGenView from '@/views/InvoiceGenView.vue'
import ViewInvoiceView from '@/views/ViewInvoiceView.vue'
import AdminUsersView from '@/views/AdminUsersView.vue'
import EntityHeadersView from '@/views/EntityHeadersView.vue'
import store from '@/store'
/* make sure you add all the navigations here */

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path:'/masters',
    name:'masters',
    meta:{type:"menu"},
    children:[
      {
        path:'/entity',
        name:'entity',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:EntityView
      },
      {
        path:'/banks',
        name:'bank',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:BanksView
      },
      {
        path:'/bank-branches',
        name:'bank branch',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:BankBranches
      },
      {
        path:'/bank-accounts',
        name:'bank account',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:BankAccounts
      },
      {
        path:'/billables',
        name:'billable',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:BillablesView
      },
      {
        path:'/payee-entities',
        name:'payee entity',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:payeeEntityView
      },
      {
        path:'/tax',
        name:'tax',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:TaxesView
      },
      {
        path:'/invoice-templates',
        name:'invoice template',
        meta:{type:"sub-item", requiresUserGroup:true},
        component:InvoiceTemplates
      },
    ]    
  },
  {
    path:'/invoice-management',
    name:'invoice management',    
    meta:{type:"menu", forbidAdmin:true},
    children:[
      {
        path:'/invoice',
        name:'invoice',
        meta:{type:"sub-item", requiresUserGroup:true},
        component: InvoiceGenView,
      },
      {
        path:'/viewinvoice',
        name:'view invoice',
        meta:{type:"sub-item", requiresUserGroup:true},
        component: ViewInvoiceView,
      },
    ]
  },
  {
    path: '/about',
    name: 'about',    
    component: AboutView,
    meta:{type:"single"}
  },
  {
    path:'/contact',
    name:'contact',
    component: contactPage,
    meta:{type:"single"}
  },
  {
    path:'/options',
    name:'options',
    meta:{type:"menu"},
    children:[
      {
        path:'/settings',
        name:'settings',
        meta:{type:"sub-item"}
      },
      {
        path:'/profile',
        name:'profile',
        meta:{type:"sub-item"}
      },
      {
        path:'/entity-headers',
        name:'entity headers',
        meta:{type:"sub-item"},
        component: EntityHeadersView,
      },
    ]

  },
  {
    path:'/administration',
    name:'administration',
    meta:{type:"menu"},
    children:[
      {
        path:'/admin/users',
        name:'admin users',
        meta:{type:"sub-item", requiresAdmin:true},
        component: AdminUsersView,
      },
    ]
  },
  

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const needsAdmin = to.matched.some((record) => record.meta?.requiresAdmin);
  const needsUserGroup = to.matched.some((record) => record.meta?.requiresUserGroup);
  const blockedForAdmin = to.matched.some((record) => record.meta?.forbidAdmin);

  if (blockedForAdmin && store.getters.isAdmin) {
    store.commit('addAlert', {
      title: 'Access Denied',
      message: 'Admin users are not allowed to access invoices.',
      type: 'danger'
    });
    next('/');
    return;
  }

  if (!needsAdmin) {
    if (needsUserGroup && store.getters.isLoggedIn && !store.getters.isUserGroupMember) {
      store.commit('addAlert', {
        title: 'Access Denied',
        message: 'Only User group members can access this page.',
        type: 'danger'
      });
      next('/');
      return;
    }

    next();
    return;
  }

  if (store.getters.isAdmin) {
    next();
    return;
  }

  store.commit('addAlert', {
    title: 'Access Denied',
    message: 'You are not allowed to access this page.',
    type: 'danger'
  });
  next('/');
});

export default router
