/**
 * Árbol de menú del sidebar (Cork multinivel).
 * anyOf: al menos un permiso. route: hoja navegable.
 */
export const ranchMenu = [
  {
    id: 'panel',
    label: 'Panel',
    icon: 'home',
    anyOf: ['ranch.dashboard', 'ranch.reports.view', 'ranch.cattle.view'],
    children: [
      {
        id: 'alertas',
        label: 'Centro de alertas',
        route: '/ganaderia/alertas',
        anyOf: ['ranch.dashboard', 'ranch.reports.view', 'ranch.cattle.view'],
      },
      {
        id: 'dashboard',
        label: 'Inicio del rancho',
        route: '/ganaderia/dashboard',
        anyOf: ['ranch.dashboard'],
      },
    ],
  },
  {
    id: 'animales',
    label: 'Animales',
    icon: 'tag',
    anyOf: ['ranch.cattle.view', 'ranch.cattle.manage', 'ranch.weight.record', 'ranch.health.view', 'ranch.health.manage'],
    children: [
      {
        id: 'inventario',
        label: 'Inventario y expediente',
        route: '/ganaderia/animales',
        anyOf: ['ranch.cattle.view', 'ranch.cattle.manage'],
      },
      {
        id: 'pesajes',
        label: 'Pesajes',
        route: '/ganaderia/pesajes',
        anyOf: ['ranch.weight.record'],
      },
      {
        id: 'pesajes-becerros',
        label: 'Pesaje becerros',
        route: '/ganaderia/pesajes?tab=becerros',
        anyOf: ['ranch.weight.record'],
      },
      {
        id: 'salud',
        label: 'Salud y vacunas',
        route: '/ganaderia/salud',
        anyOf: ['ranch.health.manage', 'ranch.health.view'],
      },
      {
        id: 'partos',
        label: 'Partos',
        route: '/ganaderia/partos',
        anyOf: ['ranch.cattle.manage'],
      },
      {
        id: 'bajas',
        label: 'Bajas y ventas',
        route: '/ganaderia/bajas',
        anyOf: ['ranch.cattle.manage'],
      },
    ],
  },
  {
    id: 'ubicacion',
    label: 'Ubicación',
    icon: 'map-pin',
    anyOf: ['ranch.cattle.view', 'ranch.cattle.manage'],
    children: [
      {
        id: 'potreros',
        label: 'Potreros y corrales',
        route: '/ganaderia/potreros',
        anyOf: ['ranch.cattle.manage'],
      },
      {
        id: 'movimientos',
        label: 'Movimientos',
        route: '/ganaderia/movimientos',
        anyOf: ['ranch.cattle.view', 'ranch.cattle.manage'],
      },
      {
        id: 'inventario-ubicacion',
        label: '¿Dónde está cada animal?',
        route: '/ganaderia/inventario-ubicacion',
        anyOf: ['ranch.cattle.view'],
      },
    ],
  },
  {
    id: 'engorda',
    label: 'Engorda',
    icon: 'layers',
    anyOf: ['ranch.fattening.manage', 'ranch.reports.view', 'ranch.expenses.manage'],
    children: [
      {
        id: 'lotes',
        label: 'Lotes en engorda',
        route: '/ganaderia/engorda',
        anyOf: ['ranch.fattening.manage'],
      },
      {
        id: 'alimentos',
        label: 'Catálogo de alimentos',
        route: '/ganaderia/alimentos',
        anyOf: ['ranch.expenses.manage', 'ranch.fattening.manage'],
      },
      {
        id: 'compras',
        label: 'Compras al inventario',
        route: '/ganaderia/compras',
        anyOf: ['ranch.expenses.manage'],
      },
      {
        id: 'proyeccion',
        label: 'Proyección de cierre',
        route: '/ganaderia/engorda-proyeccion',
        anyOf: ['ranch.fattening.manage', 'ranch.reports.view'],
      },
      {
        id: 'cerrados',
        label: 'Lotes cerrados',
        route: '/ganaderia/lotes-cerrados',
        anyOf: ['ranch.fattening.manage', 'ranch.reports.view'],
      },
    ],
  },
  {
    id: 'finanzas',
    label: 'Finanzas y reportes',
    icon: 'dollar-sign',
    anyOf: ['ranch.expenses.manage', 'ranch.reports.view', 'ranch.cattle.view'],
    children: [
      {
        id: 'gastos',
        label: 'Compras e insumos',
        route: '/ganaderia/compras',
        anyOf: ['ranch.expenses.manage'],
      },
      {
        id: 'reportes',
        label: 'Reportes',
        icon: 'bar-chart-2',
        anyOf: ['ranch.reports.view', 'ranch.cattle.view'],
        children: [
          {
            id: 'rentabilidad',
            label: 'Rentabilidad por animal',
            route: '/ganaderia/rentabilidad',
            anyOf: ['ranch.reports.view'],
          },
          {
            id: 'balance',
            label: 'Balance del periodo',
            route: '/ganaderia/reportes',
            anyOf: ['ranch.reports.view'],
          },
          {
            id: 'alertas-costo',
            label: 'Costo alto por kg',
            route: '/ganaderia/alertas-costo',
            anyOf: ['ranch.reports.view', 'ranch.cattle.view'],
          },
          {
            id: 'gdp',
            label: 'GDP — ganancia de peso',
            route: '/ganaderia/rendimiento-peso',
            anyOf: ['ranch.reports.view', 'ranch.cattle.view'],
          },
          {
            id: 'vacas-madre',
            label: 'Desempeño vacas madre',
            route: '/ganaderia/vacas-madre',
            anyOf: ['ranch.reports.view', 'ranch.cattle.view'],
          },
        ],
      },
    ],
  },
  {
    id: 'admin',
    label: 'Administración',
    icon: 'settings',
    anyOf: ['administracion.usuarios', 'administracion.roles'],
    children: [
      {
        id: 'config-empresa',
        labelKey: 'admin.config_empresa.nav',
        label: 'Config. empresa',
        route: '/admin/configuracion-empresa',
        anyOf: ['administracion.usuarios'],
      },
      {
        id: 'users',
        label: 'Usuarios',
        route: '/users/lista',
        anyOf: ['administracion.usuarios'],
      },
      {
        id: 'roles',
        label: 'Roles',
        route: '/roles/lista',
        anyOf: ['administracion.roles'],
      },
      {
        id: 'db-backup',
        label: 'Respaldo de base de datos',
        route: '/ganaderia/respaldo-db',
        anyOf: ['administracion.usuarios'],
      },
    ],
  },
]

export function collectRoutes(node, acc = []) {
  if (node.route) acc.push(node.route)
  ;(node.children || []).forEach((c) => collectRoutes(c, acc))
  return acc
}

export function pathMatchesRoute(path, route) {
  if (!route) return false
  if (path === route) return true
  // Detalles anidados (animales/:id, engorda/:id)
  if (route === '/ganaderia/animales' && path.startsWith('/ganaderia/animales/')) return true
  if (route === '/ganaderia/engorda' && path.startsWith('/ganaderia/engorda/')) return true
  return false
}
