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 quotations = sequelize.define(
    'quotations',
    {
      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"

        ],

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

valid_until: {
        type: DataTypes.DATE,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  quotations.associate = (db) => {

    db.quotations.belongsToMany(db.quotation_lines, {
      as: 'lines',
      foreignKey: {
        name: 'quotations_linesId',
      },
      constraints: false,
      through: 'quotationsLinesQuotation_lines',
    });

    db.quotations.belongsToMany(db.quotation_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'quotations_linesId',
      },
      constraints: false,
      through: 'quotationsLinesQuotation_lines',
    });


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



























    db.quotations.hasMany(db.quotation_lines, {
      as: 'quotation_lines_quotation',
      foreignKey: {
          name: 'quotationId',
      },
      constraints: false,
    });


    db.quotations.hasMany(db.sales_orders, {
      as: 'sales_orders_quotation',
      foreignKey: {
          name: 'quotationId',
      },
      constraints: false,
    });





























//end loop



    db.quotations.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

    db.quotations.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return quotations;
};


