const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const RolePermission = sequelize.define('RolePermission', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '关联ID'
  },
  role_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '角色ID'
  },
  permission_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '权限ID'
  },
  granted: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: true,
    comment: '是否授权'
  }
}, {
  tableName: 'role_permissions',
  timestamps: true,
  underscored: true,
  indexes: [
    { 
      fields: ['role_id', 'permission_id'], 
      unique: true,
      name: 'uk_role_permission'
    },
    { fields: ['role_id'] },
    { fields: ['permission_id'] }
  ]
});

module.exports = RolePermission;