module.exports = (sequelize, DataTypes) => {
  const InventoryStockLedger = sequelize.define('InventoryStockLedger', {
    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'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    transaction_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    transaction_type: {
      type: DataTypes.ENUM('RECEIPT', 'ISSUE', 'RETURN', 'SCRAP', 'ADJUSTMENT', 'TRANSFER_IN', 'TRANSFER_OUT', 'OPENING_BALANCE'),
      allowNull: false
    },
    reference_type: {
      type: DataTypes.ENUM('GRN', 'MIN', 'MRN', 'SCRAP', 'ADJUSTMENT', 'TRANSFER', 'PRODUCTION_RECEIPT', 'OTHER', 'OPENING_BALANCE'),
      allowNull: false
    },
    reference_id: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    reference_number: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    lot_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    expiry_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    quantity_in: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    quantity_out: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    balance_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    unit_cost: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_cost: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    valuation_method: {
      type: DataTypes.ENUM('FIFO', 'LIFO', 'AVERAGE', 'STANDARD'),
      allowNull: true,
      defaultValue: 'FIFO'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'inventory_stock_ledger',
    indexes: [
      { fields: ['company_id', 'plant_id', 'material_id', 'transaction_date'] },
      { fields: ['transaction_type'] },
      { fields: ['reference_type', 'reference_id'] },
      { fields: ['batch_number'] },
      { fields: ['lot_number'] },
      { fields: ['storage_location'] }
    ]
  });

  InventoryStockLedger.associate = function(models) {
    InventoryStockLedger.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    InventoryStockLedger.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    InventoryStockLedger.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    InventoryStockLedger.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
  };

  return InventoryStockLedger;
};

