'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 创建权限表
    await queryInterface.createTable('permissions', {
      id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true,
        comment: '权限ID'
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '权限名称'
      },
      code: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '权限代码'
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '权限描述'
      },
      module: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '所属模块'
      },
      type: {
        type: Sequelize.ENUM('menu', 'operation'),
        allowNull: false,
        defaultValue: 'operation',
        comment: '权限类型：menu-菜单权限，operation-操作权限'
      },
      parent_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '父权限ID',
        references: {
          model: 'permissions',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      status: {
        type: Sequelize.ENUM('active', 'inactive'),
        allowNull: false,
        defaultValue: 'active',
        comment: '状态'
      },
      sort_order: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        comment: '创建时间'
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
        comment: '更新时间'
      }
    }, {
      comment: '权限表'
    });

    // 创建索引
    await queryInterface.addIndex('permissions', ['code'], {
      name: 'idx_permissions_code'
    });
    await queryInterface.addIndex('permissions', ['module'], {
      name: 'idx_permissions_module'
    });
    await queryInterface.addIndex('permissions', ['type'], {
      name: 'idx_permissions_type'
    });
    await queryInterface.addIndex('permissions', ['parent_id'], {
      name: 'idx_permissions_parent_id'
    });
    await queryInterface.addIndex('permissions', ['status'], {
      name: 'idx_permissions_status'
    });
  },

  async down(queryInterface, Sequelize) {
    // 删除索引
    await queryInterface.removeIndex('permissions', 'idx_permissions_code');
    await queryInterface.removeIndex('permissions', 'idx_permissions_module');
    await queryInterface.removeIndex('permissions', 'idx_permissions_type');
    await queryInterface.removeIndex('permissions', 'idx_permissions_parent_id');
    await queryInterface.removeIndex('permissions', 'idx_permissions_status');
    
    // 删除表
    await queryInterface.dropTable('permissions');
  }
};