module.exports = (sequelize, DataTypes) => {
  const ProductionWipLedger = sequelize.define(
    'ProductionWipLedger',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Company', key: 'id' },
      },
      plant_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Plant', key: 'id' },
      },
      production_order_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'ProductionOrder', key: 'id' },
      },
      material_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'Material', key: 'id' },
      },
      uom_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'UOM', key: 'id' },
      },
      direction: {
        type: DataTypes.STRING(3),
        allowNull: false,
      },
      movement_type: {
        type: DataTypes.STRING(40),
        allowNull: false,
      },
      qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
      },
      route_card_operation_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'RouteCardOperation', key: 'id' },
      },
      material_issue_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'MaterialIssue', key: 'id' },
      },
      production_order_material_receipt_line_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionOrderMaterialReceiptLine', key: 'id' },
      },
      remarks: {
        type: DataTypes.STRING(500),
        allowNull: true,
      },
      created_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' },
      },
    },
    {
      tableName: 'production_wip_ledger',
      timestamps: true,
      updatedAt: false,
      createdAt: 'created_at',
      indexes: [
        { fields: ['production_order_id', 'material_id'] },
        { fields: ['route_card_operation_id'] },
      ],
    }
  );

  ProductionWipLedger.associate = (models) => {
    ProductionWipLedger.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionWipLedger.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    ProductionWipLedger.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    ProductionWipLedger.belongsTo(models.RouteCardOperation, { foreignKey: 'route_card_operation_id', as: 'routeCardOperation' });
    ProductionWipLedger.belongsTo(models.MaterialIssue, { foreignKey: 'material_issue_id', as: 'materialIssue' });
    ProductionWipLedger.belongsTo(models.ProductionOrderMaterialReceiptLine, {
      foreignKey: 'production_order_material_receipt_line_id',
      as: 'materialReceiptLine',
    });
    ProductionWipLedger.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
  };

  return ProductionWipLedger;
};
