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

credit_note_number: {
        type: DataTypes.TEXT,
      
      

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"applied",


"canceled"

        ],

      },

subtotal_ht: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_ttc: {
        type: DataTypes.DECIMAL,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  credit_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.credit_notes.hasMany(db.credit_note_items, {
      as: 'credit_note_items_credit_note',
      foreignKey: {
          name: 'credit_noteId',
      },
      constraints: false,
    });













//end loop



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

    db.credit_notes.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

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



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


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

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



  return credit_notes;
};


