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,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

quote_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requested",


"in_review",


"sent",


"accepted",


"declined",


"expired"

        ],

      },

quote_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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_items, {
      as: 'quote_items_quote',
      foreignKey: {
          name: 'quoteId',
      },
      constraints: false,
    });






//end loop



    db.quotes.belongsTo(db.accounts, {
      as: 'account',
      foreignKey: {
        name: 'accountId',
      },
      constraints: false,
    });

    db.quotes.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

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

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



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


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

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



  return quotes;
};


