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,
      
      

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

valid_until: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"sent",


"accepted",


"rejected",


"expired",


"converted"

        ],

      },

subtotal_ht: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_ttc: {
        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,
    });


    db.quotes.hasMany(db.sales_orders, {
      as: 'sales_orders_source_quote',
      foreignKey: {
          name: 'source_quoteId',
      },
      constraints: false,
    });





















//end loop



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

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

    db.quotes.belongsTo(db.payment_terms, {
      as: 'payment_terms',
      foreignKey: {
        name: 'payment_termsId',
      },
      constraints: false,
    });



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


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

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



  return quotes;
};


