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

invoice_number: {
        type: DataTypes.TEXT,
      
      

      },

invoice_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected"

        ],

      },

vat_treatment: {
        type: DataTypes.ENUM,
      
      

        values: [

"standard_15",


"zero_rated",


"exempt",


"not_applicable"

        ],

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

vat_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

withholding_tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

review_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  purchase_invoices.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_invoices.hasMany(db.purchase_invoice_lines, {
      as: 'purchase_invoice_lines_purchase_invoice',
      foreignKey: {
          name: 'purchase_invoiceId',
      },
      constraints: false,
    });










//end loop



    db.purchase_invoices.belongsTo(db.merchants, {
      as: 'merchant',
      foreignKey: {
        name: 'merchantId',
      },
      constraints: false,
    });

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

    db.purchase_invoices.belongsTo(db.receipts, {
      as: 'receipt',
      foreignKey: {
        name: 'receiptId',
      },
      constraints: false,
    });

    db.purchase_invoices.belongsTo(db.users, {
      as: 'entered_by',
      foreignKey: {
        name: 'entered_byId',
      },
      constraints: false,
    });

    db.purchase_invoices.belongsTo(db.users, {
      as: 'reviewed_by',
      foreignKey: {
        name: 'reviewed_byId',
      },
      constraints: false,
    });

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




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

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



  return purchase_invoices;
};


