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

const WarehouseTransaction = sequelize.define('WarehouseTransaction', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  materialId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: Material,
      key: 'id'
    },
    comment: '物资ID'
  },
  type: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      isIn: [['in', 'out']]
    },
    comment: '操作类型：in(入库)，out(出库)'
  },
  quantity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    validate: {
      min: 1
    },
    comment: '操作数量'
  },
  operator: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '操作人'
  },
  remark: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注'
  }
}, {
  tableName: 'warehouse_transactions',
  timestamps: true,
  paranoid: true,
  underscored: true,
  freezeTableName: true
});

// 定义关系
WarehouseTransaction.belongsTo(Material, {
  foreignKey: 'materialId',
  as: 'material'
});

module.exports = WarehouseTransaction;