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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

app_category: {
        type: DataTypes.ENUM,
      
      

        values: [

"productivity",


"education",


"moderation",


"fun",


"analytics",


"devtools",


"other"

        ],

      },

runtime: {
        type: DataTypes.ENUM,
      
      

        values: [

"wasmedge",


"wasmtime"

        ],

      },

version: {
        type: DataTypes.TEXT,
      
      

      },

wasm_artifact_uri: {
        type: DataTypes.TEXT,
      
      

      },

reliability_score: {
        type: DataTypes.DECIMAL,
      
      

      },

vetting_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"approved",


"rejected",


"disabled_auto",


"disabled_admin"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

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

  marketplace_apps.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_apps.hasMany(db.app_installations, {
      as: 'app_installations_marketplace_app',
      foreignKey: {
          name: 'marketplace_appId',
      },
      constraints: false,
    });


    db.marketplace_apps.hasMany(db.app_crash_reports, {
      as: 'app_crash_reports_marketplace_app',
      foreignKey: {
          name: 'marketplace_appId',
      },
      constraints: false,
    });











//end loop



    db.marketplace_apps.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return marketplace_apps;
};


