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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending_approval",


"approved",


"confirmed",


"picked",


"shipped",


"completed",


"cancelled"

        ],

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

requested_ship_date: {
        type: DataTypes.DATE,
      
      

      },

shipped_at: {
        type: DataTypes.DATE,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_address: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales_orders.associate = (db) => {

    db.sales_orders.belongsToMany(db.sales_order_lines, {
      as: 'lines',
      foreignKey: {
        name: 'sales_orders_linesId',
      },
      constraints: false,
      through: 'sales_ordersLinesSales_order_lines',
    });

    db.sales_orders.belongsToMany(db.sales_order_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'sales_orders_linesId',
      },
      constraints: false,
      through: 'sales_ordersLinesSales_order_lines',
    });


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





























    db.sales_orders.hasMany(db.sales_order_lines, {
      as: 'sales_order_lines_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });



    db.sales_orders.hasMany(db.invoices, {
      as: 'invoices_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });



    db.sales_orders.hasMany(db.customer_returns, {
      as: 'customer_returns_sales_order',
      foreignKey: {
          name: 'sales_orderId',
      },
      constraints: false,
    });
























//end loop



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

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

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

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




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

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



  return sales_orders;
};


