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

listing_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"beat",


"stem_pack",


"preset_pack"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"ZAR",


"USD",


"EUR",


"GBP"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"paused",


"sold_out",


"archived"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  marketplace_listings.associate = (db) => {


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



























    db.marketplace_listings.hasMany(db.marketplace_orders, {
      as: 'marketplace_orders_listing',
      foreignKey: {
          name: 'listingId',
      },
      constraints: false,
    });



//end loop



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

    db.marketplace_listings.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.marketplace_listings.belongsTo(db.genres, {
      as: 'genre',
      foreignKey: {
        name: 'genreId',
      },
      constraints: false,
    });

    db.marketplace_listings.belongsTo(db.assets, {
      as: 'preview_asset',
      foreignKey: {
        name: 'preview_assetId',
      },
      constraints: false,
    });

    db.marketplace_listings.belongsTo(db.assets, {
      as: 'deliverable_asset',
      foreignKey: {
        name: 'deliverable_assetId',
      },
      constraints: false,
    });

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




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

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



  return marketplace_listings;
};


