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 corrective_actions = sequelize.define(
    'corrective_actions',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

capa_number: {
        type: DataTypes.TEXT,
      
      

      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"corrective",


"preventive"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"investigating",


"action_planning",


"implementing",


"verifying",


"closed"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

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,
    },
  );

  corrective_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.corrective_actions.belongsTo(db.nonconformances, {
      as: 'nonconformance',
      foreignKey: {
        name: 'nonconformanceId',
      },
      constraints: false,
    });

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

    db.corrective_actions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return corrective_actions;
};


