import type { RouteRecordRaw, RouteLocationNormalized } from 'vue-router'
import { useProjectsStore } from 'stores/projects'

const setProjectBeforeEnter = (to: RouteLocationNormalized) => {
  const id = Number(to.params.id)
  const projectsStore = useProjectsStore()
  projectsStore.setCurrentProjectId(
    !isNaN(id) && projectsStore.projectById(id) ? id : null
  )
}

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: () => import('layouts/MainLayout.vue'),
    children: [
      {
        path: '',
        redirect: { name: 'projects' }
      },
      {
        name: 'projects',
        path: '/projects',
        component: () => import('pages/ProjectsPage.vue'),
        meta: { 
          hideBackButton: true,
          requiresAuth: true
        }
      },
      {
        name: 'project_add',
        path: '/project/add',
        component: () => import('pages/ProjectCreatePage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'project_info',
        path: '/project/:id(\\d+)/info',
        component: () => import('pages/ProjectInfoPage.vue'),
        beforeEnter: setProjectBeforeEnter,
        meta: { requiresAuth: true }
      },
      {
        name: 'company_mask',
        path: '/project/:id(\\d+)/company-mask',
        component: () => import('pages/CompanyMaskPage.vue'),
        beforeEnter: setProjectBeforeEnter,
        meta: { requiresAuth: true }
      },
      {
        path: '/project/:id(\\d+)',
        component: () => import('pages/ProjectPage.vue'),
        beforeEnter: setProjectBeforeEnter,
        meta: { requiresAuth: true },
        children: [
          {
            name: 'project',
            path: '',
            redirect: { name: 'chats' }
          },
          {
            name: 'chats',
            path: 'chats',
            component: () => import('pages/project-page/ProjectPageChats.vue'),
            meta: { 
              backRoute: 'projects',
              requiresAuth: true
            }
          },
          {
            name: 'persons',
            path: 'persons',
            component: () => import('pages/project-page/ProjectPagePersons.vue'),
            meta: { 
              backRoute: 'projects',
              requiresAuth: true
            }
          },
          {
            name: 'companies',
            path: 'companies',
            component: () => import('pages/project-page/ProjectPageCompanies.vue'),
            meta: { 
              backRoute: 'projects',
              requiresAuth: true
            }
          }
        ]
      },
      {
        name: 'company_info',
        path: '/project/:id(\\d+)/company/:companyId',
        component: () => import('pages/CompanyInfoPage.vue'),
        beforeEnter: setProjectBeforeEnter,
        meta: { requiresAuth: true }
      },
      {
        name: 'person_info',
        path: '/project/:id(\\d+)/person/:personId',
        component: () => import('pages/PersonInfoPage.vue'),
        beforeEnter: setProjectBeforeEnter,
        meta: { requiresAuth: true }
      },
      {
        name: 'account',
        path: '/account',
        component: () => import('pages/AccountPage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'create_account',
        path: '/create-account',
        component: () => import('src/pages/AccountCreatePage.vue'),
        meta: { 
          public: true,
          guestOnly: true 
        }
      },
      {
        name: 'change_account_password',
        path: '/change-password',
        component: () => import('pages/AccountChangePasswordPage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'change_account_email',
        path: '/change-email',
        component: () => import('pages/AccountChangeEmailPage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'subscribe',
        path: '/subscribe',
        component: () => import('pages/SubscribePage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'terms',
        path: '/terms-of-use',
        component: () => import('pages/TermsPage.vue'),
        meta: { public: true }
      },
      {
        name: 'privacy',
        path: '/privacy',
        component: () => import('pages/PrivacyPage.vue'),
        meta: { public: true }
      },
      {
        name: 'your_company',
        path: '/your-company',
        component: () => import('src/pages/CompanyYourPage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'login',
        path: '/login',
        component: () => import('pages/LoginPage.vue'),
        meta: { 
          public: true,
          guestOnly: true 
        }
      },
      {
        name: 'recovery_password',
        path: '/recovery-password',
        component: () => import('src/pages/AccountForgotPasswordPage.vue'),
        meta: { 
          public: true,
          guestOnly: true 
        }
      },
      {
        name: 'add_company',
        path: '/add-company',
        component: () => import('src/pages/CompanyCreatePage.vue'),
        meta: { requiresAuth: true }
      },
      {
        name: 'settings',
        path: '/settings',
        component: () => import('pages/SettingsPage.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/:catchAll(.*)*',
    component: () => import('pages/ErrorNotFound.vue'),
    meta: { public: true }
  }
]

export default routes