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

session_key: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"converted",


"abandoned"

        ],

      },

subtotal_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

grand_total_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  carts.associate = (db) => {

    db.carts.belongsToMany(db.cart_items, {
      as: 'items',
      foreignKey: {
        name: 'carts_itemsId',
      },
      constraints: false,
      through: 'cartsItemsCart_items',
    });

    db.carts.belongsToMany(db.cart_items, {
      as: 'items_filter',
      foreignKey: {
        name: 'carts_itemsId',
      },
      constraints: false,
      through: 'cartsItemsCart_items',
    });


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


















    db.carts.hasMany(db.cart_items, {
      as: 'cart_items_cart',
      foreignKey: {
          name: 'cartId',
      },
      constraints: false,
    });



























//end loop



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

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




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

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



  return carts;
};


