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

module.exports = (sequelize) => {
  const InternalAudit = sequelize.define(
    "internalaudit",
    {
      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 },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      doc_no: { type: DataTypes.STRING(100), allowNull: true },
      department_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "departments", key: "id"}},
      is_saved: {type: DataTypes.TINYINT, allowNull: true},
    },
    {
      tableName: "internalaudits",
      freezeTableName: true,
    }
  );

  /**
   * Associates the City model with other models.
   * @param {object} models - The models to associate with.
   */
  InternalAudit.associate = (models) => {
    InternalAudit.hasMany(models.InternalAuditItem, { foreignKey: "internalaudit_id", as: "internalaudititems" });
    InternalAudit.belongsTo(models.Department, { foreignKey: "department_id" });
  };

  return InternalAudit;
};
