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_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"completed",


"cancelled"

        ],

      },

financial_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"authorized",


"paid",


"partially_paid",


"refunded",


"partially_refunded",


"voided"

        ],

      },

fulfillment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unfulfilled",


"partial",


"fulfilled",


"restocked"

        ],

      },

subtotal_price: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_price: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_price: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total_price: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_code: {
        type: DataTypes.TEXT,
      
      

      },

placed_at: {
        type: DataTypes.DATE,
      
      

      },

cancelled_at: {
        type: DataTypes.DATE,
      
      

      },

cancel_reason: {
        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.fulfillments, {
      as: 'fulfillments_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });


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



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







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




//end loop



    db.orders.belongsTo(db.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      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;
};


