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

invoice_number: {
        type: DataTypes.TEXT,
      
      

      },

invoice_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"standard",


"credit_note",


"debit_note"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"sent",


"partially_paid",


"paid",


"overdue",


"void"

        ],

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

currency_code: {
        type: DataTypes.TEXT,
      
      

      },

exchange_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

amount_paid: {
        type: DataTypes.DECIMAL,
      
      

      },

balance_due: {
        type: DataTypes.DECIMAL,
      
      

      },

customer_notes: {
        type: DataTypes.TEXT,
      
      

      },

terms_and_conditions: {
        type: DataTypes.TEXT,
      
      

      },

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

  invoices.associate = (db) => {

    db.invoices.belongsToMany(db.invoice_lines, {
      as: 'invoice_lines',
      foreignKey: {
        name: 'invoices_invoice_linesId',
      },
      constraints: false,
      through: 'invoicesInvoice_linesInvoice_lines',
    });

    db.invoices.belongsToMany(db.invoice_lines, {
      as: 'invoice_lines_filter',
      foreignKey: {
        name: 'invoices_invoice_linesId',
      },
      constraints: false,
      through: 'invoicesInvoice_linesInvoice_lines',
    });


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
















    db.invoices.hasMany(db.invoice_lines, {
      as: 'invoice_lines_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });


    db.invoices.hasMany(db.payments, {
      as: 'payments_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });


    db.invoices.hasMany(db.receipts, {
      as: 'receipts_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });








    db.invoices.hasMany(db.efris_submissions, {
      as: 'efris_submissions_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });




//end loop



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

    db.invoices.belongsTo(db.contacts, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.invoices.belongsTo(db.quotes, {
      as: 'quote',
      foreignKey: {
        name: 'quoteId',
      },
      constraints: false,
    });



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


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

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



  return invoices;
};


