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

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

      internalaudititem_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "internalaudititems", key: "id"}},
      item_finding: { type: DataTypes.STRING, allowNull: true },
      criteria: { type: DataTypes.STRING, allowNull: true },
      term: { type: DataTypes.TEXT, allowNull: true },
      cause_analysis: { type: DataTypes.TEXT, allowNull: true },
      impact: { type: DataTypes.TEXT, allowNull: true },
      action_plan: { type: DataTypes.TEXT, allowNull: true },
      prevention: { type: DataTypes.TEXT, allowNull: true },
      start_date: { type: DataTypes.DATEONLY, allowNull: true },
      to_date: { type: DataTypes.DATEONLY, allowNull: true },
      files: { type: DataTypes.STRING, allowNull: true },
      pic: { type: DataTypes.JSON, allowNull: true },
      verification_result: { type: DataTypes.STRING, allowNull: true },
    },
    {
      tableName: "internalauditfindings",
      freezeTableName: true,
    }
  );

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

  return InternalAuditFinding;
};
