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",


"confirmed",


"fulfilled",


"canceled",


"refunded"

        ],

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

payment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unpaid",


"partial",


"paid",


"overdue"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales_orders.associate = (db) => {


/// 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,
    });














//end loop



    db.sales_orders.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.sales_orders.belongsTo(db.business_units, {
      as: 'business_unit',
      foreignKey: {
        name: 'business_unitId',
      },
      constraints: false,
    });

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




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

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



  return sales_orders;
};


