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

module.exports = (sequelize) => {
  const WorkInstruction = sequelize.define(
    "workinstruction",
    {
      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 },
      title: { type: DataTypes.STRING(100), allowNull: true },
      docno_revise: { type: DataTypes.STRING(100), allowNull: true },
      purpose: { type: DataTypes.TEXT, allowNull: true },
      scope: { type: DataTypes.TEXT, allowNull: true },
      definition: { type: DataTypes.TEXT, allowNull: true },
      responsibility: { type: DataTypes.TEXT, allowNull: true },
      reference: { type: DataTypes.TEXT, allowNull: true },
      work_instruction: { type: DataTypes.TEXT, allowNull: true },
      attachment: { type: DataTypes.TEXT, allowNull: true },
      distribution: { type: DataTypes.TEXT, allowNull: true },
      change_history: { type: DataTypes.TEXT, allowNull: true },
      approval: { type: DataTypes.TEXT, allowNull: true },
      doc_status: { type: DataTypes.STRING(100), allowNull: true },

      department_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "departments", key: "id"}},
    },
    {
      tableName: "workinstructions",
    },
    {
      freezeTableName: true,
    }
  );

  /**
   * Associates the Institution model with other models.
   * @param {object} models - The models to associate with.
   */
  WorkInstruction.associate = (models) => {
   WorkInstruction.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });
  };


  return WorkInstruction;
};
