import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useAuthStore } from './auth'

export const usePermissionStore = defineStore('permission', () => {
  const authStore = useAuthStore()
  
  // 状态
  const menuList = ref([])
  const permissionList = ref([])
  const roleList = ref([])
  const isLoading = ref(false)

  // 权限常量定义
  const PERMISSIONS = {
    // 用户管理
    USER_VIEW: 'user:view',
    USER_CREATE: 'user:create',
    USER_EDIT: 'user:edit',
    USER_DELETE: 'user:delete',
    
    // 角色管理
    ROLE_VIEW: 'role:view',
    ROLE_CREATE: 'role:create',
    ROLE_EDIT: 'role:edit',
    ROLE_DELETE: 'role:delete',
    
    // 权限管理
    PERMISSION_VIEW: 'permission:view',
    PERMISSION_CREATE: 'permission:create',
    PERMISSION_EDIT: 'permission:edit',
    PERMISSION_DELETE: 'permission:delete',
    
    // 牛只管理
    CATTLE_VIEW: 'cattle:view',
    CATTLE_CREATE: 'cattle:create',
    CATTLE_EDIT: 'cattle:edit',
    CATTLE_DELETE: 'cattle:delete',
    
    // 交易管理
    TRADING_VIEW: 'trading:view',
    TRADING_CREATE: 'trading:create',
    TRADING_EDIT: 'trading:edit',
    TRADING_DELETE: 'trading:delete',
    
    // 财务管理
    FINANCE_VIEW: 'finance:view',
    FINANCE_CREATE: 'finance:create',
    FINANCE_EDIT: 'finance:edit',
    FINANCE_DELETE: 'finance:delete',
    
    // 监控管理
    MONITOR_VIEW: 'monitor:view',
    MONITOR_MANAGE: 'monitor:manage',
    
    // 系统管理
    SYSTEM_VIEW: 'system:view',
    SYSTEM_MANAGE: 'system:manage',
    
    // 超级管理员
    ADMIN_ALL: '*'
  }

  // 角色常量定义
  const ROLES = {
    SUPER_ADMIN: 'super_admin',
    ADMIN: 'admin',
    MANAGER: 'manager',
    OPERATOR: 'operator',
    VIEWER: 'viewer'
  }

  // 默认菜单配置
  const defaultMenus = [
    {
      id: 'dashboard',
      name: '首页',
      path: '/',
      icon: 'el-icon-house',
      permission: null, // 所有用户都可以访问
      children: []
    },
    {
      id: 'monitor',
      name: '监控中心',
      path: '/monitor',
      icon: 'el-icon-monitor',
      permission: PERMISSIONS.MONITOR_VIEW,
      children: [
        {
          id: 'monitor-center',
          name: '实时监控',
          path: '/monitor-center',
          permission: PERMISSIONS.MONITOR_VIEW
        }
      ]
    },
    {
      id: 'cattle',
      name: '牛只管理',
      path: '/cattle',
      icon: 'el-icon-cow',
      permission: PERMISSIONS.CATTLE_VIEW,
      children: [
        {
          id: 'cattle-list',
          name: '牛只列表',
          path: '/cattle/list',
          permission: PERMISSIONS.CATTLE_VIEW
        },
        {
          id: 'cattle-add',
          name: '添加牛只',
          path: '/cattle/add',
          permission: PERMISSIONS.CATTLE_CREATE
        }
      ]
    },
    {
      id: 'trading',
      name: '交易管理',
      path: '/trading',
      icon: 'el-icon-goods',
      permission: PERMISSIONS.TRADING_VIEW,
      children: [
        {
          id: 'trading-list',
          name: '交易列表',
          path: '/trading/list',
          permission: PERMISSIONS.TRADING_VIEW
        },
        {
          id: 'trading-add',
          name: '新增交易',
          path: '/trading/add',
          permission: PERMISSIONS.TRADING_CREATE
        }
      ]
    },
    {
      id: 'finance',
      name: '财务管理',
      path: '/finance',
      icon: 'el-icon-money',
      permission: PERMISSIONS.FINANCE_VIEW,
      children: [
        {
          id: 'finance-records',
          name: '财务记录',
          path: '/finance/records',
          permission: PERMISSIONS.FINANCE_VIEW
        },
        {
          id: 'finance-statistics',
          name: '财务统计',
          path: '/finance/statistics',
          permission: PERMISSIONS.FINANCE_VIEW
        }
      ]
    },
    {
      id: 'system',
      name: '系统管理',
      path: '/system',
      icon: 'el-icon-setting',
      permission: PERMISSIONS.SYSTEM_VIEW,
      children: [
        {
          id: 'user-management',
          name: '用户管理',
          path: '/system/users',
          permission: PERMISSIONS.USER_VIEW
        },
        {
          id: 'role-management',
          name: '角色管理',
          path: '/system/roles',
          permission: PERMISSIONS.ROLE_VIEW
        },
        {
          id: 'permission-management',
          name: '权限管理',
          path: '/system/permissions',
          permission: PERMISSIONS.PERMISSION_VIEW
        }
      ]
    }
  ]

  // 计算属性
  const accessibleMenus = computed(() => {
    return filterMenusByPermission(defaultMenus)
  })

  const hasPermission = computed(() => (permission) => {
    if (!permission) return true // 无权限要求的菜单所有人都可以访问
    return authStore.hasPermission.value(permission)
  })

  const hasRole = computed(() => (role) => {
    return authStore.hasRole.value(role)
  })

  // 方法
  const filterMenusByPermission = (menus) => {
    return menus.filter(menu => {
      // 检查当前菜单权限
      if (menu.permission && !hasPermission.value(menu.permission)) {
        return false
      }
      
      // 递归过滤子菜单
      if (menu.children && menu.children.length > 0) {
        menu.children = filterMenusByPermission(menu.children)
      }
      
      return true
    })
  }

  const checkRoutePermission = (route) => {
    const routePath = route.path
    
    // 查找对应的菜单项
    const findMenuByPath = (menus, path) => {
      for (const menu of menus) {
        if (menu.path === path) {
          return menu
        }
        if (menu.children) {
          const found = findMenuByPath(menu.children, path)
          if (found) return found
        }
      }
      return null
    }
    
    const menuItem = findMenuByPath(defaultMenus, routePath)
    if (!menuItem) return true // 未配置的路由默认允许访问
    
    return hasPermission.value(menuItem.permission)
  }

  const getPermissionsByRole = (role) => {
    const rolePermissions = {
      [ROLES.SUPER_ADMIN]: [PERMISSIONS.ADMIN_ALL],
      [ROLES.ADMIN]: [
        PERMISSIONS.USER_VIEW, PERMISSIONS.USER_CREATE, PERMISSIONS.USER_EDIT, PERMISSIONS.USER_DELETE,
        PERMISSIONS.ROLE_VIEW, PERMISSIONS.ROLE_CREATE, PERMISSIONS.ROLE_EDIT, PERMISSIONS.ROLE_DELETE,
        PERMISSIONS.PERMISSION_VIEW, PERMISSIONS.PERMISSION_CREATE, PERMISSIONS.PERMISSION_EDIT, PERMISSIONS.PERMISSION_DELETE,
        PERMISSIONS.CATTLE_VIEW, PERMISSIONS.CATTLE_CREATE, PERMISSIONS.CATTLE_EDIT, PERMISSIONS.CATTLE_DELETE,
        PERMISSIONS.TRADING_VIEW, PERMISSIONS.TRADING_CREATE, PERMISSIONS.TRADING_EDIT, PERMISSIONS.TRADING_DELETE,
        PERMISSIONS.FINANCE_VIEW, PERMISSIONS.FINANCE_CREATE, PERMISSIONS.FINANCE_EDIT, PERMISSIONS.FINANCE_DELETE,
        PERMISSIONS.MONITOR_VIEW, PERMISSIONS.MONITOR_MANAGE,
        PERMISSIONS.SYSTEM_VIEW, PERMISSIONS.SYSTEM_MANAGE
      ],
      [ROLES.MANAGER]: [
        PERMISSIONS.USER_VIEW,
        PERMISSIONS.CATTLE_VIEW, PERMISSIONS.CATTLE_CREATE, PERMISSIONS.CATTLE_EDIT,
        PERMISSIONS.TRADING_VIEW, PERMISSIONS.TRADING_CREATE, PERMISSIONS.TRADING_EDIT,
        PERMISSIONS.FINANCE_VIEW, PERMISSIONS.FINANCE_CREATE, PERMISSIONS.FINANCE_EDIT,
        PERMISSIONS.MONITOR_VIEW
      ],
      [ROLES.OPERATOR]: [
        PERMISSIONS.CATTLE_VIEW, PERMISSIONS.CATTLE_CREATE, PERMISSIONS.CATTLE_EDIT,
        PERMISSIONS.TRADING_VIEW, PERMISSIONS.TRADING_CREATE,
        PERMISSIONS.FINANCE_VIEW,
        PERMISSIONS.MONITOR_VIEW
      ],
      [ROLES.VIEWER]: [
        PERMISSIONS.CATTLE_VIEW,
        PERMISSIONS.TRADING_VIEW,
        PERMISSIONS.FINANCE_VIEW,
        PERMISSIONS.MONITOR_VIEW
      ]
    }
    
    return rolePermissions[role] || []
  }

  const initializePermissions = () => {
    // 从认证store获取用户权限信息
    permissionList.value = authStore.permissions
    roleList.value = authStore.roles
    
    // 生成可访问的菜单
    menuList.value = accessibleMenus.value
  }

  return {
    // 状态
    menuList,
    permissionList,
    roleList,
    isLoading,
    
    // 常量
    PERMISSIONS,
    ROLES,
    
    // 计算属性
    accessibleMenus,
    hasPermission,
    hasRole,
    
    // 方法
    filterMenusByPermission,
    checkRoutePermission,
    getPermissionsByRole,
    initializePermissions
  }
})