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_title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

product_form: {
        type: DataTypes.ENUM,
      
      

        values: [

"live",


"whole",


"gutted",


"fillet",


"frozen",


"other"

        ],

      },

available_quantity_kg: {
        type: DataTypes.DECIMAL,
      
      

      },

price_per_kg: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

available_from: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

listing_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"paused",


"sold_out",


"expired",


"removed"

        ],

      },

      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.order_items, {
      as: 'order_items_listing',
      foreignKey: {
          name: 'listingId',
      },
      constraints: false,
    });












//end loop



    db.marketplace_listings.belongsTo(db.tenants, {
      as: 'seller_tenant',
      foreignKey: {
        name: 'seller_tenantId',
      },
      constraints: false,
    });

    db.marketplace_listings.belongsTo(db.harvests, {
      as: 'harvest',
      foreignKey: {
        name: 'harvestId',
      },
      constraints: false,
    });

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

    db.marketplace_listings.belongsTo(db.tenant_locations, {
      as: 'fulfillment_location',
      foreignKey: {
        name: 'fulfillment_locationId',
      },
      constraints: false,
    });

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



    db.marketplace_listings.hasMany(db.file, {
      as: 'photos',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.marketplace_listings.getTableName(),
        belongsToColumn: 'photos',
      },
    });


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

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



  return marketplace_listings;
};


