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

product_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"ARTICLE",


"COMIC",


"VIDEO",


"GAME"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

price_supercharged: {
        type: DataTypes.INTEGER,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

max_purchases: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"DRAFT",


"PUBLISHED",


"ARCHIVED"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  products.associate = (db) => {


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

















    db.products.hasMany(db.article_contents, {
      as: 'article_contents_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.comic_contents, {
      as: 'comic_contents_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.video_contents, {
      as: 'video_contents_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.game_contents, {
      as: 'game_contents_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.product_purchases, {
      as: 'product_purchases_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.content_consumptions, {
      as: 'content_consumptions_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });








//end loop



    db.products.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });



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


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

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



  return products;
};


