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'
import RoleManagementView from '../views/RoleManagementView.vue'
import DepartmentManagementView from '../views/DepartmentManagementView.vue'
import SystemConfigView from '../views/SystemConfigView.vue'
import type { AdminRole } from '../auth/roles'

// 路由权限配置
const routeRoles: Record<string, AdminRole[]> = {
  'dashboard': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member', 'finance_manager', 'finance_member', 'risk_manager', 'risk_member', 'customer_service', 'auditor', 'viewer'],
  'activities': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member', 'customer_service', 'auditor', 'viewer'],
  'activity-create': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member'],
  'activity-detail': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member', 'customer_service', 'auditor', 'viewer'],
  'activity-config': ['super_admin', 'system_admin', 'operation_manager', 'marketing_manager'],
  'users': ['super_admin', 'system_admin'],
  'user-detail': ['super_admin', 'system_admin'],
  'user-invite': ['super_admin', 'system_admin'],
  'rewards': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member', 'finance_manager', 'finance_member', 'auditor', 'viewer'],
  'risk': ['super_admin', 'system_admin', 'risk_manager', 'risk_member', 'auditor'],
  'audit': ['super_admin', 'system_admin', 'finance_manager', 'auditor'],
  'approvals': ['super_admin', 'system_admin', 'operation_manager', 'marketing_manager', 'finance_manager', 'risk_manager'],
  'permissions': ['super_admin', 'system_admin'],
  'role-management': ['super_admin', 'system_admin'],
  'department-management': ['super_admin', 'system_admin'],
  'system-config': ['super_admin', 'system_admin', 'auditor'],
  'notifications': ['super_admin', 'system_admin', 'operation_manager', 'operation_member', 'marketing_manager', 'marketing_member', 'finance_manager', 'finance_member', 'risk_manager', 'risk_member', 'customer_service', 'auditor', 'viewer']
}

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

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

export default router
