module.exports = (sequelize, DataTypes) => {
  const Notification = sequelize.define('Notification', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    company_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'Company', key: 'id' } },
    user_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'User', key: 'id' }, comment: 'User who will receive the notification' },
    type: { type: DataTypes.ENUM('APPROVAL_REQUEST', 'APPROVAL_APPROVED', 'APPROVAL_REJECTED', 'SYSTEM', 'INFO', 'WARNING', 'ERROR'), allowNull: false, defaultValue: 'INFO' },
    module_name: { type: DataTypes.STRING(50), allowNull: true, comment: 'Module name (e.g., PurchaseOrder, SalesOrder)' },
    transaction_id: { type: DataTypes.INTEGER, allowNull: true, comment: 'ID of the transaction (e.g., PO ID, SO ID)' },
    transaction_number: { type: DataTypes.STRING(50), allowNull: true, comment: 'Transaction number for reference' },
    title: { type: DataTypes.STRING(200), allowNull: false },
    message: { type: DataTypes.TEXT, allowNull: false },
    link: { type: DataTypes.STRING(500), allowNull: true, comment: 'URL to navigate when notification is clicked' },
    is_read: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
    read_at: { type: DataTypes.DATE, allowNull: true },
    priority: { type: DataTypes.ENUM('LOW', 'MEDIUM', 'HIGH', 'URGENT'), allowNull: false, defaultValue: 'MEDIUM' },
    created_by: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'User', key: 'id' } },
    updated_by: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'User', key: 'id' } },
  }, { 
    tableName: 'notification',
    indexes: [
      { fields: ['user_id', 'is_read'] },
      { fields: ['company_id', 'user_id'] },
      { fields: ['type', 'module_name'] },
      { fields: ['created_at'] },
    ]
  });

  Notification.associate = function(models) {
    Notification.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Notification.belongsTo(models.User, { foreignKey: 'user_id', as: 'user' });
    Notification.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    Notification.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };
  
  return Notification;
};

