import {createRouter, createWebHistory} from 'vue-router'
import Dashboard from '@/views/Dashboard.vue';
import Login from '@/views/Login.vue';
import Register from '@/views/Register.vue';
import Pairing from '@/views/Pairing.vue';
import store from '@/store';
import Profile from '@/views/Profile.vue';
import Settings from '@/views/Settings.vue';
import Friends from '@/views/Friends.vue';
import Groups from '@/views/Groups.vue';
import GroupNew from '@/views/GroupNew.vue';
import GroupDetail from '@/views/GroupDetail.vue';
import Inventory from '@/views/Inventory.vue';
import Search from '@/views/Search.vue';
import InventoryDetail from '@/views/InventoryDetail.vue';
import InventoryDetailForeign from '@/views/InventoryDetailForeign.vue';
import InventoryNew from '@/views/InventoryNew.vue';
import InventoryEdit from '@/views/InventoryEdit.vue';
import StorageLocation from '@/views/StorageLocation.vue';
import StorageLocationDetail from '@/views/StorageLocationDetail.vue';
import StorageLocationNew from '@/views/StorageLocationNew.vue';
import StorageLocationEdit from '@/views/StorageLocationEdit.vue';
import Admin from '@/views/Admin.vue';
import Swatch from '@/views/Swatch.vue';
import Files from '@/views/Files.vue';
import Print from "@/views/Print.vue";
import Workflows from '@/views/Workflows.vue';
import WorkflowDetail from '@/views/WorkflowDetail.vue';

import Account from '@/views/settings/Account.vue';
import Password from '@/views/settings/Password.vue';
import Privacy from '@/views/settings/Privacy.vue';
import Email from '@/views/settings/Email.vue';
import Notifications from '@/views/settings/Notifications.vue';
import Data from '@/views/settings/Data.vue';
import Preferences from '@/views/settings/Preferences.vue';


const routes = [{path: '/', component: Dashboard, meta: {requiresAuth: true}}, {
    path: '/inventory',
    component: Inventory,
    meta: {requiresAuth: true}
}, {
    path: '/inventory/:id',
    component: InventoryDetail,
    meta: {requiresAuth: true},
    props: true
}, {
    path: '/inventory/:id/edit',
    component: InventoryEdit,
    meta: {requiresAuth: true},
    props: true
}, {
    path: '/inventory/shared/:user/:id',
    component: InventoryDetailForeign,
    meta: {requiresAuth: true, foreign: true},
    props: true
}, {
    path: '/i/:handle/:id',
    redirect: to => {
        const {handle, id} = to.params
        return handle === store.state.user ? '/inventory/' + id : '/inventory/shared/' + handle + '/' + id
    }
}, {path: '/inventory/new', component: InventoryNew, meta: {requiresAuth: true}}, {
    path: '/friends',
    component: Friends,
    meta: {requiresAuth: true}
}, {
    path: '/groups',
    component: Groups,
    meta: {requiresAuth: true}
}, {
    path: '/groups/new',
    component: GroupNew,
    meta: {requiresAuth: true}
}, {
    path: '/groups/:id',
    component: GroupDetail,
    meta: {requiresAuth: true},
    props: true
}, {path: '/files', component: Files, meta: {requiresAuth: true}
}, {path: '/workflows',
    component: Workflows,
    meta: {requiresAuth: true},
}, {path: '/workflows/:id/:phase?',
    name: 'workflow-detail',
    component: WorkflowDetail,
    meta: {requiresAuth: true},
    props: true
}, {path: '/admin',
    component: Admin,
    meta: {requiresAuth: true}
}, {path: '/swatch', component: Swatch, meta: {requiresAuth: true}}, {
    path: '/print',
    component: Print,
    meta: {requiresAuth: true},
    props: route => {
        const {kind, ...components} = route.query;
        return {prefill: kind ? {kind, components} : null};
    }
}, {
    path: '/search/:query',
    component: Search,
    meta: {requiresAuth: true},
    props: true
}, {path: '/login', component: Login, meta: {requiresAuth: false}}, {
    path: '/register',
    component: Register,
    meta: {requiresAuth: false}
}, {path: '/pairing', component: Pairing, meta: {requiresAuth: false}}, {
    path: '/profile',
    component: Profile,
    meta: {requiresAuth: true}
}, {
    path: '/settings', component: Settings, meta: {requiresAuth: true}, children: [{
        path: '', name: 'account', component: Account, meta: {requiresAuth: true}
    },{
        path: 'password/', name: 'password', component: Password, meta: {requiresAuth: true}
    }, {
        path: 'privacy/', name: 'privacy', component: Privacy, meta: {requiresAuth: true}
    }, {
        path: 'email/', name: 'email', component: Email, meta: {requiresAuth: true}
    }, {
        path: 'notifications/', name: 'notifications', component: Notifications, meta: {requiresAuth: true}
    }, {
        path: 'data/', name: 'data', component: Data, meta: {requiresAuth: true}
    }, {
        path: 'preferences/', name: 'preferences', component: Preferences, meta: {requiresAuth: true}
    }]
}, {
    path: '/storage-location',
    component: StorageLocation,
    meta: {requiresAuth: true}
}, {
    path: '/storage-locations/:id',
    component: StorageLocationDetail,
    meta: {requiresAuth: true},
    props: true
}, {
    path: '/storage-locations/:id/edit',
    component: StorageLocationEdit,
    meta: {requiresAuth: true},
    props: true
}, {
    path: '/storage-locations/new',
    component: StorageLocationNew,
    meta: {requiresAuth: true}
}, {path: '/:pathMatch(.*)*', redirect: '/'}]

const router = createRouter({
    history: createWebHistory(), linkActiveClass: "active", routes,
})

router.beforeEach((to/*, from*/) => {
    // if this route requires auth, check if logged in, if not, redirect to login page.
    if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
        console.log("Not logged in, redirecting to login page")
        return {
            path: '/login', // save the location we were at to come back later
            query: {redirect: to.fullPath},
        }
    }
})

export default router