import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import { usePermissionStore } from '@/store'

/**页面组件 */
const MainLayout = () => import('@/layouts/MainLayout.vue')
const Dashboard = () => import('@/views/Dashboard.vue')
const Editor = () => import('@/views/editor/Editor.vue')
const Statistics = () => import('@/views/Statistics.vue')
const Chat = () => import('@/views/chat/index.vue')
const Templates = () => import('@/views/TemplateDetail.vue')
const QuickStart = () => import('@/views/QuickStart.vue')
const Docs = () => import('@/views/Docs.vue')
const About = () => import('@/views/About.vue')
const UserCenter = () => import('@/views/UserCenter.vue')
const LogStream = () => import('@/views/LogStream.vue')
const ModelLog = () => import('@/views/ModelLog.vue')
const WorkflowExecution = () => import('@/views/WorkflowExecution.vue')
const FlowManagement = () => import('@/views/flow/index.vue')
const ModelManager = () => import('@/views/model/index.vue')
const ModelPage = () => import('@/views/model/index.vue')
const OllamaPage = () => import('@/views/ollama/index.vue')
const KnowledgeManager = () => import('@/views/knowledge/index.vue')
const AgentManager = () => import('@/views/agent/index.vue')
const PromptPage = () => import('@/views/prompt/index.vue')
const WebSearchPage = () => import('@/views/web-search/index.vue')
const McpPage = () => import('@/views/mcp/index.vue')
const StoragePage = () => import('@/views/storage/index.vue')
const VectorStorePage = () => import('@/views/vector/index.vue')
const SkillsPage = () => import('@/views/skills/index.vue')
const Workspace = () => import('@/views/workspace/index.vue')
const UnauthorizedPage = () => import('@/views/system/401.vue')
const NotFoundPage = () => import('@/views/system/404.vue')

const routes = [
	{
		path: '/',
		component: MainLayout,
		children: [
			{
				path: '',
				name: 'Dashboard',
				component: Dashboard,
				alias: 'overview'
			},
			{
				path: 'statistics',
				name: 'Statistics',
				component: Statistics
			},
			{
				path: 'templates/:id',
				name: 'TemplateDetail',
				component: Templates
			},
			{
				path: 'execution',
				name: 'WorkflowExecution',
				component: WorkflowExecution,
				meta: { menuCode: 'sys_ai_exec_logs' }
			},
			{
				path: 'agent',
				name: 'FlowManagement',
				component: AgentManager,
				meta: { menuCode: 'sys_ai_agent' }
			},
			{
				path: 'quick-start',
				name: 'QuickStart',
				component: QuickStart
			},
			{
				path: 'docs',
				name: 'Docs',
				component: Docs
			},
			{
				path: 'about',
				name: 'About',
				component: About
			},
			{
				path: 'user-center',
				name: 'UserCenter',
				component: UserCenter
			},
			{
				path: 'log-stream',
				name: 'LogStream',
				component: LogStream
			},
			{
				path: 'model-log',
				name: 'ModelLog',
				component: ModelLog
			},
			{
				path: 'model',
				name: 'ModelManager',
				component: ModelManager,
				meta: { menuCode: 'sys_ai_models' }
			},
			{
				path: 'models',
				name: 'ModelPage',
				component: ModelPage,
				meta: { menuCode: 'sys_ai_models' }
			},
			{
				path: 'ollama',
				name: 'OllamaPage',
				component: OllamaPage,
				meta: { menuCode: 'sys_ai_ollama' }
			},
			{
				path: 'knowledge',
				name: 'KnowledgeManager',
				component: KnowledgeManager,
				meta: { menuCode: 'sys_ai_knowledge_base' }
			},
			{
				path: 'prompts',
				name: 'PromptPage',
				component: PromptPage,
				meta: { menuCode: 'sys_ai_prompts' }
			},
			{
				path: 'storage',
				name: 'StoragePage',
				component: StoragePage,
				meta: { menuCode: 'sys_ai_storage' }
			},
			{
				path: 'vector-store',
				name: 'VectorStorePage',
				component: VectorStorePage,
				meta: { menuCode: 'sys_ai_vector_store' }
			},
			{
				path: 'web-search',
				name: 'WebSearchPage',
				component: WebSearchPage,
				meta: { menuCode: 'sys_ai_web_search' }
			},
			{
				path: 'mcp',
				name: 'McpPage',
				component: McpPage,
				meta: { menuCode: 'sys_ai_mcp' }
			},
			{
				path: 'skills',
				name: 'SkillsPage',
				component: SkillsPage,
				meta: { menuCode: 'sys_ai_skills' }
			},
			{
				path: 'workspace',
				name: 'Workspace',
				component: Workspace
			},
			{
				path: '/workflow',
				name: 'Workflow',
				component: FlowManagement,
				meta: { menuCode: 'sys_ai_workflow' }
			}
		]
	},
	{
		path: '/workflow/:id',
		name: 'Editor',
		component: Editor
	},
	{
		path: '/chat',
		name: 'Chat',
		component: Chat,
		meta: { menuCode: 'sys_ai_chat' }
	},
	{
		path: '/401',
		name: 'Unauthorized',
		component: UnauthorizedPage
	},
	{
		path: '/404',
		name: 'NotFound',
		component: NotFoundPage
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

router.beforeEach(async (to, _from, next) => {
	NProgress.start()
	const permissionStore = usePermissionStore()
	const menuCode = to.meta.menuCode

	if (!menuCode) {
		next()
		return
	}

	try {
		await permissionStore.initializePermissions()

		if (!permissionStore.hasMenuAccess(menuCode)) {
			next('/401')
			return
		}

		await permissionStore.ensureButtonPermissions(menuCode)
		next()
	} catch {
		next()
	}
})

router.addRoute({
	path: '/:pathMatch(.*)*',
	redirect: '/404'
})

router.afterEach(() => {
	NProgress.done()
})

export default router
