'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 插入基础权限数据
    await queryInterface.bulkInsert('permissions', [
      // 系统管理模块权限
      {
        name: '系统管理',
        code: 'system:manage',
        description: '系统管理模块访问权限',
        module: 'system',
        type: 'menu',
        parent_id: null,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '用户管理',
        code: 'system:user:manage',
        description: '用户管理页面访问权限',
        module: 'system',
        type: 'menu',
        parent_id: 1,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '用户查看',
        code: 'system:user:view',
        description: '查看用户列表权限',
        module: 'system',
        type: 'operation',
        parent_id: 2,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '用户新增',
        code: 'system:user:create',
        description: '新增用户权限',
        module: 'system',
        type: 'operation',
        parent_id: 2,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '用户编辑',
        code: 'system:user:edit',
        description: '编辑用户权限',
        module: 'system',
        type: 'operation',
        parent_id: 2,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '用户删除',
        code: 'system:user:delete',
        description: '删除用户权限',
        module: 'system',
        type: 'operation',
        parent_id: 2,
        status: 'active',
        sort_order: 4,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '角色管理',
        code: 'system:role:manage',
        description: '角色管理页面访问权限',
        module: 'system',
        type: 'menu',
        parent_id: 1,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '角色查看',
        code: 'system:role:view',
        description: '查看角色列表权限',
        module: 'system',
        type: 'operation',
        parent_id: 7,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '角色新增',
        code: 'system:role:create',
        description: '新增角色权限',
        module: 'system',
        type: 'operation',
        parent_id: 7,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '角色编辑',
        code: 'system:role:edit',
        description: '编辑角色权限',
        module: 'system',
        type: 'operation',
        parent_id: 7,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '角色删除',
        code: 'system:role:delete',
        description: '删除角色权限',
        module: 'system',
        type: 'operation',
        parent_id: 7,
        status: 'active',
        sort_order: 4,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '权限管理',
        code: 'system:permission:manage',
        description: '权限管理页面访问权限',
        module: 'system',
        type: 'menu',
        parent_id: 1,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '权限查看',
        code: 'system:permission:view',
        description: '查看权限列表权限',
        module: 'system',
        type: 'operation',
        parent_id: 12,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '权限分配',
        code: 'system:permission:assign',
        description: '分配权限给角色权限',
        module: 'system',
        type: 'operation',
        parent_id: 12,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },

      // 保险管理模块权限
      {
        name: '保险管理',
        code: 'insurance:manage',
        description: '保险管理模块访问权限',
        module: 'insurance',
        type: 'menu',
        parent_id: null,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '保险申请管理',
        code: 'insurance:application:manage',
        description: '保险申请管理页面访问权限',
        module: 'insurance',
        type: 'menu',
        parent_id: 15,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '保险申请查看',
        code: 'insurance:application:view',
        description: '查看保险申请权限',
        module: 'insurance',
        type: 'operation',
        parent_id: 16,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '保险申请审核',
        code: 'insurance:application:review',
        description: '审核保险申请权限',
        module: 'insurance',
        type: 'operation',
        parent_id: 16,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '保单管理',
        code: 'insurance:policy:manage',
        description: '保单管理页面访问权限',
        module: 'insurance',
        type: 'menu',
        parent_id: 15,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '保单查看',
        code: 'insurance:policy:view',
        description: '查看保单权限',
        module: 'insurance',
        type: 'operation',
        parent_id: 19,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '理赔管理',
        code: 'insurance:claim:manage',
        description: '理赔管理页面访问权限',
        module: 'insurance',
        type: 'menu',
        parent_id: 15,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '理赔查看',
        code: 'insurance:claim:view',
        description: '查看理赔权限',
        module: 'insurance',
        type: 'operation',
        parent_id: 21,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '理赔审核',
        code: 'insurance:claim:review',
        description: '审核理赔权限',
        module: 'insurance',
        type: 'operation',
        parent_id: 21,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },

      // 监管任务模块权限
      {
        name: '监管任务',
        code: 'supervision:manage',
        description: '监管任务模块访问权限',
        module: 'supervision',
        type: 'menu',
        parent_id: null,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '监管任务查看',
        code: 'supervision:task:view',
        description: '查看监管任务权限',
        module: 'supervision',
        type: 'operation',
        parent_id: 24,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '监管任务创建',
        code: 'supervision:task:create',
        description: '创建监管任务权限',
        module: 'supervision',
        type: 'operation',
        parent_id: 24,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '监管任务分配',
        code: 'supervision:task:assign',
        description: '分配监管任务权限',
        module: 'supervision',
        type: 'operation',
        parent_id: 24,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },

      // 设备管理模块权限
      {
        name: '设备管理',
        code: 'device:manage',
        description: '设备管理模块访问权限',
        module: 'device',
        type: 'menu',
        parent_id: null,
        status: 'active',
        sort_order: 4,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '设备查看',
        code: 'device:view',
        description: '查看设备权限',
        module: 'device',
        type: 'operation',
        parent_id: 28,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '设备新增',
        code: 'device:create',
        description: '新增设备权限',
        module: 'device',
        type: 'operation',
        parent_id: 28,
        status: 'active',
        sort_order: 2,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '设备编辑',
        code: 'device:edit',
        description: '编辑设备权限',
        module: 'device',
        type: 'operation',
        parent_id: 28,
        status: 'active',
        sort_order: 3,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '设备删除',
        code: 'device:delete',
        description: '删除设备权限',
        module: 'device',
        type: 'operation',
        parent_id: 28,
        status: 'active',
        sort_order: 4,
        created_at: new Date(),
        updated_at: new Date()
      },

      // 日志管理模块权限
      {
        name: '日志管理',
        code: 'log:manage',
        description: '日志管理模块访问权限',
        module: 'log',
        type: 'menu',
        parent_id: null,
        status: 'active',
        sort_order: 5,
        created_at: new Date(),
        updated_at: new Date()
      },
      {
        name: '操作日志查看',
        code: 'log:operation:view',
        description: '查看操作日志权限',
        module: 'log',
        type: 'operation',
        parent_id: 33,
        status: 'active',
        sort_order: 1,
        created_at: new Date(),
        updated_at: new Date()
      }
    ], {});
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('permissions', null, {});
  }
};