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

const Position = sequelize.define('Position', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  department_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Department,
      key: 'id'
    },
    comment: '所属部门ID'
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '岗位名称'
  },
  has_permission: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
    comment: '是否已设置权限'
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '创建人ID'
  },
  updated_by: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '更新人ID'
  }
}, {
  tableName: 'government_positions',
  indexes: [
    {
      name: 'idx_position_department_id',
      fields: ['department_id']
    },
    {
      name: 'idx_position_name',
      fields: ['name']
    }
  ]
});

// 定义关系
Department.hasMany(Position, {
  foreignKey: 'department_id',
  as: 'positions'
});

Position.belongsTo(Department, {
  foreignKey: 'department_id',
  as: 'department'
});

module.exports = Position;