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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

order_channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"admin",


"reseller_storefront",


"affiliate_link",


"manual_import",


"api"

        ],

      },

order_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending_payment",


"paid",


"processing",


"packed",


"shipped",


"delivered",


"cancelled",


"refunded"

        ],

      },

subtotal_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

payment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unpaid",


"paid",


"partial",


"refunded"

        ],

      },

fulfillment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unfulfilled",


"partial",


"fulfilled",


"returned"

        ],

      },

shipping_courier: {
        type: DataTypes.TEXT,
      
      

      },

shipping_service: {
        type: DataTypes.TEXT,
      
      

      },

tracking_number: {
        type: DataTypes.TEXT,
      
      

      },

ordered_at: {
        type: DataTypes.DATE,
      
      

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

shipped_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

shipping_address: {
        type: DataTypes.TEXT,
      
      

      },

customer_note: {
        type: DataTypes.TEXT,
      
      

      },

internal_note: {
        type: DataTypes.TEXT,
      
      

      },

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

  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.orders.hasMany(db.order_items, {
      as: 'order_items_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });


    db.orders.hasMany(db.invoices, {
      as: 'invoices_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });


    db.orders.hasMany(db.payments, {
      as: 'payments_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });




    db.orders.hasMany(db.commissions, {
      as: 'commissions_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });











//end loop



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

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

    db.orders.belongsTo(db.users, {
      as: 'seller_user',
      foreignKey: {
        name: 'seller_userId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

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




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

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



  return orders;
};


