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

path: {
        type: DataTypes.TEXT,
      
      

      },

language: {
        type: DataTypes.TEXT,
      
      

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

content_hash: {
        type: DataTypes.TEXT,
      
      

      },

last_modified_at: {
        type: DataTypes.DATE,
      
      

      },

is_binary: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  code_files.associate = (db) => {


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





























//end loop



    db.code_files.belongsTo(db.code_repositories, {
      as: 'repository',
      foreignKey: {
        name: 'repositoryId',
      },
      constraints: false,
    });

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



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


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

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



  return code_files;
};


