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

title: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"proposed",


"applied",


"rolled_back",


"failed"

        ],

      },

created_at_time: {
        type: DataTypes.DATE,
      
      

      },

applied_at: {
        type: DataTypes.DATE,
      
      

      },

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

  code_change_sets.associate = (db) => {

    db.code_change_sets.belongsToMany(db.code_patches, {
      as: 'patch_files',
      foreignKey: {
        name: 'code_change_sets_patch_filesId',
      },
      constraints: false,
      through: 'code_change_setsPatch_filesCode_patches',
    });

    db.code_change_sets.belongsToMany(db.code_patches, {
      as: 'patch_files_filter',
      foreignKey: {
        name: 'code_change_sets_patch_filesId',
      },
      constraints: false,
      through: 'code_change_setsPatch_filesCode_patches',
    });


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




















    db.code_change_sets.hasMany(db.code_patches, {
      as: 'code_patches_change_set',
      foreignKey: {
          name: 'change_setId',
      },
      constraints: false,
    });










//end loop



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

    db.code_change_sets.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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




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

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



  return code_change_sets;
};


