/**
 * 添加缺失的菜单数据
 * @file add-missing-menus.js
 * @description 添加智能设备、智能预警等缺失的菜单项
 */

const { MenuPermission } = require('../models');

async function addMissingMenus() {
  try {
    console.log('开始添加缺失的菜单数据...');

    // 定义需要添加的菜单数据
    const menusToAdd = [
      // 系统概览
      {
        menu_key: 'dashboard',
        menu_name: '系统概览',
        menu_path: '/dashboard',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'dashboard-outlined',
        sort_order: 1,
        is_visible: true,
        is_enabled: true,
        description: '系统概览和统计信息'
      },
      
      // 智能设备管理
      {
        menu_key: 'smart_devices',
        menu_name: '智能设备管理',
        menu_path: '/smart-devices',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'setting-outlined',
        sort_order: 2,
        is_visible: true,
        is_enabled: true,
        description: '智能设备管理'
      },
      {
        menu_key: 'smart_eartag',
        menu_name: '智能耳标',
        menu_path: '/smart-devices/eartag',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 1,
        is_visible: true,
        is_enabled: true,
        description: '智能耳标管理'
      },
      {
        menu_key: 'smart_collar',
        menu_name: '智能项圈',
        menu_path: '/smart-devices/collar',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 2,
        is_visible: true,
        is_enabled: true,
        description: '智能项圈管理'
      },
      {
        menu_key: 'smart_host',
        menu_name: '智能主机',
        menu_path: '/smart-devices/host',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 3,
        is_visible: true,
        is_enabled: true,
        description: '智能主机管理'
      },
      {
        menu_key: 'electronic_fence',
        menu_name: '电子围栏',
        menu_path: '/smart-devices/fence',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 4,
        is_visible: true,
        is_enabled: true,
        description: '电子围栏管理'
      },

      // 智能预警管理
      {
        menu_key: 'smart_alerts',
        menu_name: '智能预警管理',
        menu_path: '/smart-alerts',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'alert-outlined',
        sort_order: 3,
        is_visible: true,
        is_enabled: true,
        description: '智能预警管理'
      },
      {
        menu_key: 'smart_eartag_alert',
        menu_name: '智能耳标预警',
        menu_path: '/smart-alerts/eartag',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 1,
        is_visible: true,
        is_enabled: true,
        description: '智能耳标预警管理'
      },
      {
        menu_key: 'smart_collar_alert',
        menu_name: '智能项圈预警',
        menu_path: '/smart-alerts/collar',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 2,
        is_visible: true,
        is_enabled: true,
        description: '智能项圈预警管理'
      },

      // 牛只管理
      {
        menu_key: 'cattle_management',
        menu_name: '牛只管理',
        menu_path: '/cattle-management',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'bug-outlined',
        sort_order: 4,
        is_visible: true,
        is_enabled: true,
        description: '牛只管理'
      },
      {
        menu_key: 'cattle_archives',
        menu_name: '牛只档案',
        menu_path: '/cattle-management/archives',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 1,
        is_visible: true,
        is_enabled: true,
        description: '牛只档案管理'
      },
      {
        menu_key: 'cattle_pens',
        menu_name: '栏舍设置',
        menu_path: '/cattle-management/pens',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 2,
        is_visible: true,
        is_enabled: true,
        description: '栏舍设置管理'
      },
      {
        menu_key: 'cattle_batches',
        menu_name: '批次设置',
        menu_path: '/cattle-management/batches',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 3,
        is_visible: true,
        is_enabled: true,
        description: '批次设置管理'
      },
      {
        menu_key: 'cattle_transfer_records',
        menu_name: '转栏记录',
        menu_path: '/cattle-management/transfer-records',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 4,
        is_visible: true,
        is_enabled: true,
        description: '转栏记录管理'
      },
      {
        menu_key: 'cattle_exit_records',
        menu_name: '离栏记录',
        menu_path: '/cattle-management/exit-records',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager", "operator"]',
        icon: 'file-outlined',
        sort_order: 5,
        is_visible: true,
        is_enabled: true,
        description: '离栏记录管理'
      },

      // 养殖场管理
      {
        menu_key: 'farm_management',
        menu_name: '养殖场管理',
        menu_path: '/farm-management',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin", "manager"]',
        icon: 'home-outlined',
        sort_order: 5,
        is_visible: true,
        is_enabled: true,
        description: '养殖场管理'
      },
      {
        menu_key: 'farm_info_management',
        menu_name: '养殖场信息管理',
        menu_path: '/farm-management/info',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager"]',
        icon: 'file-outlined',
        sort_order: 1,
        is_visible: true,
        is_enabled: true,
        description: '养殖场信息管理'
      },
      {
        menu_key: 'pen_management',
        menu_name: '栏舍管理',
        menu_path: '/farm-management/pens',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin", "manager"]',
        icon: 'file-outlined',
        sort_order: 2,
        is_visible: true,
        is_enabled: true,
        description: '栏舍管理'
      },
      {
        menu_key: 'user_management',
        menu_name: '用户管理',
        menu_path: '/farm-management/users',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin"]',
        icon: 'file-outlined',
        sort_order: 3,
        is_visible: true,
        is_enabled: true,
        description: '用户管理'
      },
      {
        menu_key: 'role_permission_management',
        menu_name: '角色权限管理',
        menu_path: '/farm-management/role-permissions',
        parent_id: null, // 将在后面更新
        menu_type: 'page',
        required_roles: '["admin"]',
        icon: 'safety-certificate-outlined',
        sort_order: 4,
        is_visible: true,
        is_enabled: true,
        description: '角色权限管理'
      },

      // 系统管理
      {
        menu_key: 'system_management',
        menu_name: '系统管理',
        menu_path: '/system',
        parent_id: null,
        menu_type: 'page',
        required_roles: '["admin"]',
        icon: 'setting-outlined',
        sort_order: 6,
        is_visible: true,
        is_enabled: true,
        description: '系统管理'
      }
    ];

    // 创建菜单
    const createdMenus = [];
    for (const menuData of menusToAdd) {
      try {
        const menu = await MenuPermission.create(menuData);
        createdMenus.push(menu);
        console.log(`创建菜单: ${menu.menu_name} (ID: ${menu.id})`);
      } catch (error) {
        if (error.name === 'SequelizeUniqueConstraintError') {
          console.log(`菜单已存在: ${menuData.menu_name}`);
        } else {
          console.error(`创建菜单失败 ${menuData.menu_name}:`, error.message);
        }
      }
    }

    // 更新子菜单的parent_id
    const menuMap = new Map();
    createdMenus.forEach(menu => {
      menuMap.set(menu.menu_key, menu.id);
    });

    // 更新智能设备子菜单
    const smartDeviceMenus = [
      { key: 'smart_eartag', parent: 'smart_devices' },
      { key: 'smart_collar', parent: 'smart_devices' },
      { key: 'smart_host', parent: 'smart_devices' },
      { key: 'electronic_fence', parent: 'smart_devices' }
    ];

    for (const menu of smartDeviceMenus) {
      const menuId = menuMap.get(menu.key);
      const parentId = menuMap.get(menu.parent);
      if (menuId && parentId) {
        await MenuPermission.update(
          { parent_id: parentId },
          { where: { id: menuId } }
        );
        console.log(`更新菜单关系: ${menu.key} -> ${menu.parent}`);
      }
    }

    // 更新智能预警子菜单
    const smartAlertMenus = [
      { key: 'smart_eartag_alert', parent: 'smart_alerts' },
      { key: 'smart_collar_alert', parent: 'smart_alerts' }
    ];

    for (const menu of smartAlertMenus) {
      const menuId = menuMap.get(menu.key);
      const parentId = menuMap.get(menu.parent);
      if (menuId && parentId) {
        await MenuPermission.update(
          { parent_id: parentId },
          { where: { id: menuId } }
        );
        console.log(`更新菜单关系: ${menu.key} -> ${menu.parent}`);
      }
    }

    // 更新牛只管理子菜单
    const cattleMenus = [
      { key: 'cattle_archives', parent: 'cattle_management' },
      { key: 'cattle_pens', parent: 'cattle_management' },
      { key: 'cattle_batches', parent: 'cattle_management' },
      { key: 'cattle_transfer_records', parent: 'cattle_management' },
      { key: 'cattle_exit_records', parent: 'cattle_management' }
    ];

    for (const menu of cattleMenus) {
      const menuId = menuMap.get(menu.key);
      const parentId = menuMap.get(menu.parent);
      if (menuId && parentId) {
        await MenuPermission.update(
          { parent_id: parentId },
          { where: { id: menuId } }
        );
        console.log(`更新菜单关系: ${menu.key} -> ${menu.parent}`);
      }
    }

    // 更新养殖场管理子菜单
    const farmMenus = [
      { key: 'farm_info_management', parent: 'farm_management' },
      { key: 'pen_management', parent: 'farm_management' },
      { key: 'user_management', parent: 'farm_management' },
      { key: 'role_permission_management', parent: 'farm_management' }
    ];

    for (const menu of farmMenus) {
      const menuId = menuMap.get(menu.key);
      const parentId = menuMap.get(menu.parent);
      if (menuId && parentId) {
        await MenuPermission.update(
          { parent_id: parentId },
          { where: { id: menuId } }
        );
        console.log(`更新菜单关系: ${menu.key} -> ${menu.parent}`);
      }
    }

    console.log('菜单数据添加完成！');
    console.log(`共创建了 ${createdMenus.length} 个菜单项`);

  } catch (error) {
    console.error('添加菜单数据失败:', error);
  } finally {
    process.exit(0);
  }
}

// 运行脚本
addMissingMenus();
