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

requisition_number: {
        type: DataTypes.TEXT,
      
      

      },

requested_date: {
        type: DataTypes.DATE,
      
      

      },

needed_by: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"cancelled",


"converted_to_po"

        ],

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

estimated_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  purchase_requisitions.associate = (db) => {


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














    db.purchase_requisitions.hasMany(db.purchase_requisition_lines, {
      as: 'purchase_requisition_lines_purchase_requisition',
      foreignKey: {
          name: 'purchase_requisitionId',
      },
      constraints: false,
    });


    db.purchase_requisitions.hasMany(db.purchase_orders, {
      as: 'purchase_orders_source_requisition',
      foreignKey: {
          name: 'source_requisitionId',
      },
      constraints: false,
    });


















//end loop



    db.purchase_requisitions.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.purchase_requisitions.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.purchase_requisitions.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });

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



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


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

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



  return purchase_requisitions;
};


