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

module.exports = (sequelize) => {
  const InternalAuditItem = sequelize.define(
    "internalaudititem",
    {
      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 },

      internalaudit_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: "internalaudits", key: "id" },
      },
      item_check: { type: DataTypes.STRING, allowNull: true },
      item_category: { type: DataTypes.STRING, allowNull: true },
      is_compliant: { type: DataTypes.BOOLEAN, allowNull: true },
      is_saved: { type: DataTypes.TINYINT, allowNull: true },
      order_no: { type: DataTypes.INTEGER, allowNull: true },
      term: { type: DataTypes.TEXT, allowNull: true },

      remarks: { type: DataTypes.TEXT, allowNull: true },
    },
    {
      tableName: "internalaudititems",
      freezeTableName: true,
    }
  );

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

  return InternalAuditItem;
};
