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,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending_payment",


"confirmed",


"processing",


"shipped",


"delivered",


"cancelled",


"refunded"

        ],

      },

payment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unpaid",


"paid",


"partially_refunded",


"refunded"

        ],

      },

fulfillment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unfulfilled",


"partially_fulfilled",


"fulfilled"

        ],

      },

subtotal_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

grand_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

customer_note: {
        type: DataTypes.TEXT,
      
      

      },

placed_at: {
        type: DataTypes.DATE,
      
      

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

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

  orders.associate = (db) => {

    db.orders.belongsToMany(db.order_items, {
      as: 'items',
      foreignKey: {
        name: 'orders_itemsId',
      },
      constraints: false,
      through: 'ordersItemsOrder_items',
    });

    db.orders.belongsToMany(db.order_items, {
      as: 'items_filter',
      foreignKey: {
        name: 'orders_itemsId',
      },
      constraints: false,
      through: 'ordersItemsOrder_items',
    });

    db.orders.belongsToMany(db.shipments, {
      as: 'shipments',
      foreignKey: {
        name: 'orders_shipmentsId',
      },
      constraints: false,
      through: 'ordersShipmentsShipments',
    });

    db.orders.belongsToMany(db.shipments, {
      as: 'shipments_filter',
      foreignKey: {
        name: 'orders_shipmentsId',
      },
      constraints: false,
      through: 'ordersShipmentsShipments',
    });

    db.orders.belongsToMany(db.payments, {
      as: 'payments',
      foreignKey: {
        name: 'orders_paymentsId',
      },
      constraints: false,
      through: 'ordersPaymentsPayments',
    });

    db.orders.belongsToMany(db.payments, {
      as: 'payments_filter',
      foreignKey: {
        name: 'orders_paymentsId',
      },
      constraints: false,
      through: 'ordersPaymentsPayments',
    });


/// 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.shipments, {
      as: 'shipments_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });


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



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



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


















//end loop



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

    db.orders.belongsTo(db.addresses, {
      as: 'shipping_address',
      foreignKey: {
        name: 'shipping_addressId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.coupons, {
      as: 'applied_coupon',
      foreignKey: {
        name: 'applied_couponId',
      },
      constraints: false,
    });




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

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



  return orders;
};


