const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const HistoryEmail = sequelize.define(
    "historyemail",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },

      recipient_email: {
        // Menyimpan alamat email penerima
        type: DataTypes.STRING,
        allowNull: false,
      },
      subject: {
        // Menyimpan subjek email
        type: DataTypes.STRING,
        allowNull: false,
      },
      body: {
        // Menyimpan isi email
        type: DataTypes.TEXT,
        allowNull: false,
      },
      status: {
        // Menyimpan status pengiriman email
        type: DataTypes.ENUM("sent", "failed", "pending"), // Status email
        allowNull: false,
        defaultValue: "pending", // Default status saat email dibuat
      },
      error_message: {
        // Menyimpan pesan error jika pengiriman gagal
        type: DataTypes.STRING,
        allowNull: true,
      },

      leaveapplication_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      
      upcountry_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },

      attendancerequest_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      
      absenceclarification_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      
      permitEase_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
    },
    {
      tableName: "historyemails",
      freezeTableName: true,
    }
  );

  /**
   * Associates the HistoryEmail model with other models.
   * @param {object} models - The models to associate with.
   */
  HistoryEmail.associate = (models) => {
    HistoryEmail.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
  };

  return HistoryEmail;
};
