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

slip_number: {
        type: DataTypes.TEXT,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

delivery_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_sent",


"queued",


"sent",


"failed"

        ],

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

delivery_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  payslips.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.payslips.belongsTo(db.payroll_items, {
      as: 'payroll_item',
      foreignKey: {
        name: 'payroll_itemId',
      },
      constraints: false,
    });

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



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


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

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



  return payslips;
};


