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

import Dashboard from './Dashboard.vue'

import { Wrench01Icon } from '@hugeicons/core-free-icons'
import { LeftToRightListDashIcon } from '@hugeicons/core-free-icons'
import { CellsIcon } from '@hugeicons/core-free-icons'
import { DashboardSquare01Icon } from '@hugeicons/core-free-icons'

const routes = [
  {
    path: '/',
    name: 'Actions',
    component: Dashboard,
    meta: { title: 'Actions', icon: DashboardSquare01Icon }
  },
  {
    path: '/dashboards/:title/:entityType?/:entityKey?',
    name: 'Dashboard',
    component: Dashboard,
    props: true,
    meta: { title: 'Dashboard' }
  },
  {
    path: '/actionBinding/:bindingId/argumentForm',
    name: 'ActionBinding',
    component: () => import('./views/ArgumentForm.vue'),
    props: true,
    meta: { title: 'Action Binding' }
  },
  {
    path: '/logs',
    name: 'Logs',
    component: () => import('./views/LogsListView.vue'),
    meta: {
      title: 'Logs',
      icon: LeftToRightListDashIcon
    }
  },
  {
    path: '/logs/calendar',
    name: 'LogsCalendar',
    component: () => import('./views/LogsCalendarView.vue'),
    meta: {
      title: 'Logs Calendar',
      breadcrumb: [
        { name: "Logs", href: "/logs" },
        { name: "Calendar" },
      ]
    }
  },
  {
    path: '/logs/queue',
    name: 'LogsQueue',
    component: () => import('./views/LogsQueueView.vue'),
    meta: {
      title: 'Execution Queue',
      breadcrumb: [
        { name: "Logs", href: "/logs" },
        { name: "Queue" },
      ]
    }
  },
  {
    path: '/entities',
    name: 'Entities',
    component: () => import('./views/EntitiesView.vue'),
    meta: {
      title: 'Entities',
      icon: CellsIcon
    }
  },
  {
    path: '/entity-details/:entityType/:entityKey',
    name: 'EntityDetails',
    component: () => import('./views/EntityDetailsView.vue'),
    props: true,
    meta: {
      title: 'OliveTin - Entity Details',
      breadcrumb: [
        { name: "Entities", href: "/entities" },
        { name: "Entity Details" }
      ]
    }
  },
  {
    path: '/logs/:executionTrackingId',
    name: 'Execution',
    component: () => import('./views/ExecutionView.vue'),
    props: true,
    meta: {
      title: 'Execution',
      breadcrumb: [
        { name: "Logs", href: "/logs" },
        { name: "Execution" },
      ]
    }
  },
  {
    path: '/action/:actionId',
    name: 'ActionDetails',
    component: () => import('./views/ActionDetailsView.vue'),
    props: true,
    meta: {
      title: 'Action Details',
      breadcrumb: [
        { name: "Actions", href: "/" },
        { name: "Action Details" },
      ]
    }
  },
  {
    path: '/action/:actionId/actionexecconditions',
    name: 'ActionExecConditions',
    component: () => import('./views/ActionExecConditionsView.vue'),
    props: true,
    meta: {
      title: 'Execution conditions',
      breadcrumb: [
        { name: "Actions", href: "/" },
        { name: "Execution conditions" },
      ]
    }
  },
  {
    path: '/diagnostics',
    name: 'Diagnostics',
    component: () => import('./views/DiagnosticsView.vue'),
    meta: {
      title: 'Diagnostics',
      icon: Wrench01Icon
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('./views/LoginView.vue'),
    meta: { title: 'Login' }
  },
  {
    path: '/user',
    name: 'UserInformation',
    component: () => import('./views/UserControlPanel.vue'),
    meta: { title: 'User Information' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('./views/NotFoundView.vue'),
    meta: { title: 'Page Not Found' }
  }
]

// Create router instance
const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// Navigation guard to update page title
router.beforeEach((to) => {
  if (to.meta && to.meta.title) {
    const pageTitle = window.initResponse?.pageTitle || 'OliveTin'
    document.title = to.meta.title + " - " + pageTitle
  }
})

// Navigation guard for authentication (if needed)
router.beforeEach((to) => {
  const isAuthenticated = window.isAuthenticated ?? false

  if (to.meta.requiresAuth && !isAuthenticated) {
    return '/login'
  }
})

export default router
