module.exports = (sequelize, DataTypes) => {
  const NotificationSetting = sequelize.define('NotificationSetting', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    auto_email_po: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    manual_email_po: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    auto_whatsapp_po: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    manual_whatsapp_po: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    email_subject_template: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    email_body_template: {
      type: DataTypes.TEXT('long'),
      allowNull: true
    },
    whatsapp_message_template: {
      type: DataTypes.TEXT('long'),
      allowNull: true
    },
    smtp_host: {
      type: DataTypes.STRING(120),
      allowNull: true
    },
    smtp_port: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    smtp_secure: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    smtp_user: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    smtp_password: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    smtp_from_email: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    smtp_from_name: {
      type: DataTypes.STRING(150),
      allowNull: true
    },
    whatsapp_api_url: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    whatsapp_api_token: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    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_setting',
    indexes: [
      { unique: true, fields: ['company_id'] },
      { fields: ['status'] }
    ]
  });

  NotificationSetting.associate = function(models) {
    NotificationSetting.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    NotificationSetting.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    NotificationSetting.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return NotificationSetting;
};
