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

delivery_note_number: {
        type: DataTypes.TEXT,
      
      

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"delivered",


"canceled"

        ],

      },

carrier_name: {
        type: DataTypes.TEXT,
      
      

      },

tracking_reference: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  delivery_notes.associate = (db) => {


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





















    db.delivery_notes.hasMany(db.delivery_note_items, {
      as: 'delivery_note_items_delivery_note',
      foreignKey: {
          name: 'delivery_noteId',
      },
      constraints: false,
    });


















//end loop



    db.delivery_notes.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.delivery_notes.belongsTo(db.sales_orders, {
      as: 'sales_order',
      foreignKey: {
        name: 'sales_orderId',
      },
      constraints: false,
    });

    db.delivery_notes.belongsTo(db.clients, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

    db.delivery_notes.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });



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


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

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



  return delivery_notes;
};


