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

/**页面组件 */
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/FlowManagement.vue')
const ModelManager = () => import('@/views/model/index.vue')
const ModelPage = () => import('@/views/model/ModelPage.vue')
const OllamaPage = () => import('@/views/model/OllamaPage.vue')
const KnowledgeManager = () => import('@/views/knowledge/index.vue')
const AgentManager = () => import('@/views/agent/index.vue')
const ResourceManager = () => import('@/views/resource/index.vue')
const PromptPage = () => import('@/views/resource/PromptPage.vue')
const WebSearchPage = () => import('@/views/resource/WebSearchPage.vue')
const McpPage = () => import('@/views/resource/McpPage.vue')
const StoragePage = () => import('@/views/resource/StoragePage.vue')
const SkillsPage = () => import('@/views/resource/SkillsPage.vue')
const Workspace = () => import('@/views/workspace/index.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
			},
			{
				path: 'agent',
				name: 'FlowManagement',
				component: AgentManager
			},
			{
				path: 'chat',
				name: 'Chat',
				component: Chat
			},
			{
				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
			},
			{
				path: 'models',
				name: 'ModelPage',
				component: ModelPage
			},
			{
				path: 'ollama',
				name: 'OllamaPage',
				component: OllamaPage
			},
			{
				path: 'knowledge',
				name: 'KnowledgeManager',
				component: KnowledgeManager
			},
			{
				path: 'resource',
				name: 'ResourceManager',
				component: ResourceManager
			},
			{
				path: 'prompts',
				name: 'PromptPage',
				component: PromptPage
			},
			{
				path: 'storage',
				name: 'StoragePage',
				component: StoragePage
			},
			{
				path: 'web-search',
				name: 'WebSearchPage',
				component: WebSearchPage
			},
			{
				path: 'mcp',
				name: 'McpPage',
				component: McpPage
			},
			{
				path: 'skills',
				name: 'SkillsPage',
				component: SkillsPage
			},
			{
				path: 'workspace',
				name: 'Workspace',
				component: Workspace
			}
		]
	},
	{
		path: '/workflow',
		name: 'Workflow',
		component: FlowManagement
	},
	{
		path: '/workflow/:id',
		name: 'Editor',
		component: Editor
	}
]

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

router.beforeEach((to, from, next) => {
	NProgress.start()
	// if (!('enterpriseCode' in to.query) && 'enterpriseCode' in from.query) {
	// 	next({
	// 		...to,
	// 		query: {
	// 			...to.query,
	// 			enterpriseCode: from.query.enterpriseCode
	// 		}
	// 	})
	// 	return
	// }
	next()
})

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

export default router
