const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const capa_actions = sequelize.define(
    'capa_actions',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

capa_number: {
        type: DataTypes.TEXT,
      
      

      },

action_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"corrective",


"preventive"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"investigating",


"implementing",


"verifying",


"closed",


"cancelled"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

root_cause: {
        type: DataTypes.TEXT,
      
      

      },

action_plan: {
        type: DataTypes.TEXT,
      
      

      },

effectiveness_check: {
        type: DataTypes.TEXT,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  capa_actions.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





































//end loop



    db.capa_actions.belongsTo(db.nonconformances, {
      as: 'nonconformance',
      foreignKey: {
        name: 'nonconformanceId',
      },
      constraints: false,
    });

    db.capa_actions.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



    db.capa_actions.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.capa_actions.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


    db.capa_actions.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.capa_actions.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return capa_actions;
};


