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,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"root_cause",


"action_planning",


"implementing",


"verifying",


"closed",


"cancelled"

        ],

      },

problem_statement: {
        type: DataTypes.TEXT,
      
      

      },

root_cause: {
        type: DataTypes.TEXT,
      
      

      },

corrective_action: {
        type: DataTypes.TEXT,
      
      

      },

preventive_action: {
        type: DataTypes.TEXT,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

      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.plants, {
      as: 'plant',
      foreignKey: {
        name: 'plantId',
      },
      constraints: false,
    });

    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.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      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;
};


