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

quote_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"sent",


"accepted",


"rejected",


"expired",


"cancelled"

        ],

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

valid_until: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

terms: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  quotes.associate = (db) => {


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









    db.quotes.hasMany(db.quote_line_items, {
      as: 'quote_line_items_quote',
      foreignKey: {
          name: 'quoteId',
      },
      constraints: false,
    });


    db.quotes.hasMany(db.projects, {
      as: 'projects_quote',
      foreignKey: {
          name: 'quoteId',
      },
      constraints: false,
    });


















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






//end loop



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

    db.quotes.belongsTo(db.leads, {
      as: 'lead',
      foreignKey: {
        name: 'leadId',
      },
      constraints: false,
    });

    db.quotes.belongsTo(db.users, {
      as: 'prepared_by',
      foreignKey: {
        name: 'prepared_byId',
      },
      constraints: false,
    });




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

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



  return quotes;
};


