import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/userStore'

const BASE_URL = (import.meta as unknown as { env: { BASE_URL?: string } }).env?.BASE_URL || '/'
const router = createRouter({
    history: createWebHistory(BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('../views/HomeView.vue'),
        },
        {
            path: '/about',
            name: 'about',
            component: () => import('../views/AboutView.vue'),
        },
        {
            path: '/pricing',
            name: 'pricing',
            component: () => import('../views/PricingView.vue'),
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue'),
            meta: { guestOnly: true },
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('../views/RegisterView.vue'),
            meta: { guestOnly: true },
        },
        {
            path: '/organization',
            name: 'organization',
            component: () => import('../views/OrganizationsView.vue'),
            meta: { requiresAuth: true },
        },
        {
            path: '/organization/:organizationUuid',
            name: 'organization-detail',
            component: () => import('../views/OrganizationView.vue'),
            meta: { requiresAuth: true },
        },
        {
            path: '/organization/:organizationUuid/manage',
            name: 'organization-manage',
            component: () => import('../views/OrganizationManage.vue'),
            meta: { requiresAuth: true, requiresManager: true },
        },
        {
            path: '/invite/:inviteUuid',
            name: 'invite-accept',
            component: () => import('../views/InviteAccept.vue'),
        },
        {
            path: '/agents',
            name: 'agents',
            component: () => import('../views/AgentsView.vue'),
            meta: { requiresAuth: true, requiresManager: true },
        },
        {
            path: '/agents/:agentUuid',
            name: 'agent-detail',
            component: () => import('../views/AgentDetailView.vue'),
            meta: { requiresAuth: true, requiresManager: true },
        },
        {
            path: '/onboarding/:roleId',
            name: 'onboarding-role',
            component: () => import('../views/OnboardingView.vue'),
            meta: { requiresAuth: true },
        },
        {
            path: '/progress',
            name: 'progress',
            component: () => import('../views/ProgressView.vue'),
            meta: { requiresAuth: true },
        },
        {
            path: '/progress/:roleId',
            name: 'progress-role',
            component: () => import('../views/ProgressDetailView.vue'),
            meta: { requiresAuth: true },
        },
        {
            path: '/:pathMatch(.*)*',
            name: 'not-found',
            component: () => import('../views/NotFoundView.vue'),
        },
    ],
})

router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    const isAuthenticated = userStore.isAuthenticated
    const isManager = userStore.isGeneralManager

    if (to.meta?.guestOnly && isAuthenticated) {
        return next({ path: '/' })
    }
    if (to.meta?.requiresAuth && !isAuthenticated) {
        return next({ path: '/login', query: { redirect: to.fullPath } })
    }
    if (to.meta?.requiresManager && !isManager) {
        return next({ path: '/' })
    }

    return next()
})

export default router
