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

name: {
        type: DataTypes.TEXT,
      
      

      },

model_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"layout_engine",


"background_generator",


"engagement_insights",


"quality_control"

        ],

      },

deployment_stage: {
        type: DataTypes.ENUM,
      
      

        values: [

"dev",


"shadow",


"canary",


"stable",


"rolled_back"

        ],

      },

model_version: {
        type: DataTypes.TEXT,
      
      

      },

artifact_uri: {
        type: DataTypes.TEXT,
      
      

      },

trained_at: {
        type: DataTypes.DATE,
      
      

      },

deployed_at: {
        type: DataTypes.DATE,
      
      

      },

rollback_trigger_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  layout_models.associate = (db) => {


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
































    db.layout_models.hasMany(db.federated_learning_updates, {
      as: 'federated_learning_updates_layout_model',
      foreignKey: {
          name: 'layout_modelId',
      },
      constraints: false,
    });
























//end loop



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

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




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

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



  return layout_models;
};


