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

po_number: {
        type: DataTypes.TEXT,
      
      

      },

total_amount_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"GBP"

        ],

      },

total_amount_original: {
        type: DataTypes.DECIMAL,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expected_delivery_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"partially_received",


"received",


"cancelled"

        ],

      },

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

  purchase_orders.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_orders.hasMany(db.po_lines, {
      as: 'po_lines_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });


    db.purchase_orders.hasMany(db.goods_receipt_notes, {
      as: 'goods_receipt_notes_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });



    db.purchase_orders.hasMany(db.service_acceptance_notes, {
      as: 'service_acceptance_notes_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });


    db.purchase_orders.hasMany(db.discrepancy_cases, {
      as: 'discrepancy_cases_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });









//end loop



    db.purchase_orders.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.purchase_orders.belongsTo(db.requisitions, {
      as: 'requisition',
      foreignKey: {
        name: 'requisitionId',
      },
      constraints: false,
    });

    db.purchase_orders.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

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



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


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

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



  return purchase_orders;
};


