import { createRouter, createWebHistory } from 'vue-router'

import Login from '@/views/Login.vue'
import Main from '../layouts/Main.vue'
import { useAdminsStore } from '@/stores/admins'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/',
            redirect: '/courses',
             component: Main,
            children: [
                {
                    path: '/pages',
                    redirect: '/pages/main',
                    children: [
                        {
                            path: 'main',
                            name: 'main',
                            component: () => import('@/views/HeaderMenu.vue')
                        },
                        {
                            path: 'about-us',
                            name: 'about-us',
                            component: () => import('@/views/About.vue')
                        },
                        {
                            path: 'faq',
                            name: 'Faq',
                            component: () => import('@/views/Faq.vue')
                        },
    
                    ]

                },
                {
                    path: '/courses',
                    name: 'courses',
                    component: () => import('@/views/Courses.vue'),
                },
                {
                    path: '/create',
                    name: 'create',
                    component: () => import('@/views/Create.vue'),
                },
                {
                    path: '/videos',
                    name: 'Videos',
                    component: () => import('@/views/Videos.vue'),
                },
                {
                    path: '/videos/:id',
                    name: 'AddVideos',
                    component: () => import('@/views/AddVideos.vue'),
                },
                {
                    path: '/:id',
                    name: 'Edit',
                    component: () => import('@/views/Edit.vue'),
                },
                {
                    path: '/coupons',
                    name: 'Coupons',
                    component: () => import('@/views/Coupons.vue'),
                },
                {
                    path: '/CreateCoupon',
                    name: 'CreateCoupon',
                    component: () => import('@/views/CreateCoupon.vue'),
                },
                {
                    path: '/coupon/:id',
                    name: 'EditCoupon',
                    component: () => import('@/views/EditCoupon.vue'),
                },
                {
                    path: '/couponsHistory',
                    name: 'couponsHistory',
                    component: () => import('@/views/couponsHistory.vue'),
                },
                {
                    path: '/categories',
                    name: 'Categories',
                    component: () => import('@/views/Categories.vue'),
                },
                {
                    path: '/admins',
                    name: 'admins',
                    component: () => import('@/views/Admins.vue'),
                },   
                {
                    path: '/users',
                    name: 'Users',
                    component: () => import('@/views/Users.vue'),
                },
                {
                    path: '/tickets',
                    name: 'Tickets',
                    component: () => import('@/views/Tickets.vue'),
                },
                {
                    path: '/tickets/:id',
                    name: 'ShowTickets',
                    component: () => import('@/views/ShowTickets.vue'),
                },
                {
                    path: '/comments',
                    name: 'Comments',
                    component: () => import('@/views/Comments.vue'),
                },
                {
                    path: '/comments/:id',
                    name: 'Answer',
                    component: () => import('@/views/AnswerComment.vue'),
                },
            ],
        }
    ]
})


router.beforeEach(function (to, from, next) {
    const admins = useAdminsStore()
    if (!admins.isLoggedIn && to.name != 'Login')
        next({ name: 'Login' })
    else if (admins.isLoggedIn && to.name == 'Login')
        next({ name: from.name })
    else next()
})


export default router
