/**
 * @file 路由测试工具
 * @description 用于验证所有路由配置是否正确，便于调试
 */

import { mainRoutes } from '../router/routes'

/**
 * 测试路由配置
 */
export const testRoutes = () => {
  console.log('🔍 开始测试路由配置...')
  
  const results = {
    total: 0,
    valid: 0,
    invalid: 0,
    errors: []
  }
  
  // 测试主路由
  mainRoutes.forEach((route, index) => {
    results.total++
    console.log(`\n📋 测试路由 ${index + 1}: ${route.name || '未命名'}`)
    
    try {
      // 检查路径
      if (!route.path) {
        throw new Error('路由缺少 path 属性')
      }
      console.log(`✅ 路径: ${route.path}`)
      
      // 检查组件
      if (route.component) {
        console.log(`✅ 组件: ${typeof route.component}`)
      } else if (route.redirect) {
        console.log(`✅ 重定向: ${route.redirect}`)
      } else {
        throw new Error('路由缺少 component 或 redirect 属性')
      }
      
      // 检查子路由
      if (route.children && route.children.length > 0) {
        console.log(`✅ 子路由数量: ${route.children.length}`)
        
        route.children.forEach((child, childIndex) => {
          console.log(`  📱 子路由 ${childIndex + 1}: ${child.name || '未命名'}`)
          console.log(`    路径: ${child.path}`)
          console.log(`    组件: ${typeof child.component}`)
          
          if (child.meta) {
            console.log(`    标题: ${child.meta.title || '无标题'}`)
            console.log(`    权限: ${child.meta.allAuthority?.join(', ') || '无权限要求'}`)
          }
        })
      }
      
      results.valid++
      
    } catch (error) {
      results.invalid++
      results.errors.push({
        route: route.name || route.path,
        error: error.message
      })
      console.error(`❌ 错误: ${error.message}`)
    }
  })
  
  // 输出测试结果
  console.log('\n📊 路由测试结果:')
  console.log(`总路由数: ${results.total}`)
  console.log(`有效路由: ${results.valid}`)
  console.log(`无效路由: ${results.invalid}`)
  
  if (results.errors.length > 0) {
    console.log('\n❌ 发现的问题:')
    results.errors.forEach((error, index) => {
      console.log(`${index + 1}. ${error.route}: ${error.error}`)
    })
  } else {
    console.log('\n🎉 所有路由配置正确！')
  }
  
  return results
}

/**
 * 测试路由导航
 */
export const testNavigation = () => {
  console.log('\n🧭 测试导航配置...')
  
  const testPaths = [
    '/productList',
    '/product/p1001',
    '/cart',
    '/checkout'
  ]
  
  testPaths.forEach(path => {
    console.log(`\n🔗 测试路径: ${path}`)
    
    // 查找匹配的路由
    const matchedRoute = findRouteByPath(path, mainRoutes)
    
    if (matchedRoute) {
      console.log(`✅ 找到路由: ${matchedRoute.name || '未命名'}`)
      if (matchedRoute.meta) {
        console.log(`   标题: ${matchedRoute.meta.title}`)
        console.log(`   描述: ${matchedRoute.meta.description}`)
      }
    } else {
      console.log(`❌ 未找到匹配的路由`)
    }
  })
}

/**
 * 根据路径查找路由
 */
const findRouteByPath = (path, routes) => {
  for (const route of routes) {
    // 检查当前路由
    if (route.path === path) {
      return route
    }
    
    // 检查子路由
    if (route.children) {
      for (const child of route.children) {
        // 处理动态路由
        if (child.path.includes(':')) {
          const pattern = new RegExp('^' + child.path.replace(/:[^/]+/g, '[^/]+') + '$')
          if (pattern.test(path)) {
            return child
          }
        } else if (child.path === path) {
          return child
        }
      }
    }
  }
  return null
}

/**
 * 获取所有可用路径
 */
export const getAllPaths = () => {
  const paths = []
  
  const extractPaths = (routes) => {
    routes.forEach(route => {
      if (route.path && !route.redirect) {
        paths.push(route.path)
      }
      if (route.children) {
        extractPaths(route.children)
      }
    })
  }
  
  extractPaths(mainRoutes)
  return paths
}

/**
 * 运行完整测试
 */
export const runFullTest = () => {
  console.log('🚀 开始运行完整路由测试...\n')
  
  // 测试路由配置
  const routeResults = testRoutes()
  
  // 测试导航
  testNavigation()
  
  // 获取所有路径
  const allPaths = getAllPaths()
  console.log('\n📋 所有可用路径:')
  allPaths.forEach((path, index) => {
    console.log(`${index + 1}. ${path}`)
  })
  
  console.log('\n✨ 路由测试完成！')
  
  return {
    routeResults,
    allPaths
  }
}

export default {
  testRoutes,
  testNavigation,
  getAllPaths,
  runFullTest
} 