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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"converted",


"abandoned"

        ],

      },

last_activity_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  carts.associate = (db) => {


/// 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: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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

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




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

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



  return carts;
};


