import { createRouter, createWebHistory } from 'vue-router'
import DashboardView from '../views/DashboardView.vue'
import ActivityListView from '../views/ActivityListView.vue'
import ForbiddenView from '../views/ForbiddenView.vue'
import LoginView from '../views/LoginView.vue'
import { useAuthStore } from '../stores/auth'
import UsersView from '../views/UsersView.vue'
import RewardsView from '../views/RewardsView.vue'
import RiskView from '../views/RiskView.vue'
import AuditLogView from '../views/AuditLogView.vue'
import NotificationsView from '../views/NotificationsView.vue'
import ActivityCreateView from '../views/ActivityCreateView.vue'
import InviteUserView from '../views/InviteUserView.vue'
import ActivityDetailView from '../views/ActivityDetailView.vue'
import ActivityConfigWizardView from '../views/ActivityConfigWizardView.vue'
import ApprovalCenterView from '../views/ApprovalCenterView.vue'
import UserDetailView from '../views/UserDetailView.vue'
import PermissionsView from '../views/PermissionsView.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/',
      name: 'dashboard',
      component: DashboardView,
      meta: { roles: ['admin', 'operator', 'viewer'] }
    },
    {
      path: '/activities',
      name: 'activities',
      component: ActivityListView,
      meta: { roles: ['admin', 'operator', 'viewer'] }
    },
    {
      path: '/activities/new',
      name: 'activity-create',
      component: ActivityCreateView,
      meta: { roles: ['admin', 'operator'] }
    },
    {
      path: '/activities/:id',
      name: 'activity-detail',
      component: ActivityDetailView,
      meta: { roles: ['admin', 'operator', 'viewer'] }
    },
    {
      path: '/activities/config',
      name: 'activity-config',
      component: ActivityConfigWizardView,
      meta: { roles: ['admin', 'operator'] }
    },
    {
      path: '/activities/:id',
      name: 'activity-detail',
      component: ActivityDetailView,
      meta: { roles: ['admin', 'operator', 'viewer'] }
    },
    {
      path: '/users',
      name: 'users',
      component: UsersView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/users/:id',
      name: 'user-detail',
      component: UserDetailView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/users/invite',
      name: 'user-invite',
      component: InviteUserView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/rewards',
      name: 'rewards',
      component: RewardsView,
      meta: { roles: ['admin', 'operator'] }
    },
    {
      path: '/risk',
      name: 'risk',
      component: RiskView,
      meta: { roles: ['admin', 'operator'] }
    },
    {
      path: '/audit',
      name: 'audit',
      component: AuditLogView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/approvals',
      name: 'approvals',
      component: ApprovalCenterView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/permissions',
      name: 'permissions',
      component: PermissionsView,
      meta: { roles: ['admin'] }
    },
    {
      path: '/notifications',
      name: 'notifications',
      component: NotificationsView,
      meta: { roles: ['admin', 'operator', 'viewer'] }
    },
    {
      path: '/403',
      name: 'forbidden',
      component: ForbiddenView
    }
  ]
})

router.beforeEach(async (to) => {
  const auth = useAuthStore()
  if (!auth.isAuthenticated && to.name !== 'login') {
    await auth.loginDemo('admin')
  }
  const roles = (to.meta?.roles as string[] | undefined) ?? null
  if (roles && !roles.includes(auth.role)) {
    return { name: 'forbidden' }
  }
  return true
})

export default router
