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

capa_number: {
        type: DataTypes.TEXT,
      
      

      },

capa_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"corrective",


"preventive"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"in_progress",


"effectiveness_check",


"closed"

        ],

      },

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_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

    db.capas.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });



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


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

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



  return capas;
};


