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

name: {
        type: DataTypes.TEXT,
      
      

      },

repo_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"local",


"git",


"generated"

        ],

      },

remote_url: {
        type: DataTypes.TEXT,
      
      

      },

default_branch: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"ready",


"syncing",


"error"

        ],

      },

last_sync_at: {
        type: DataTypes.DATE,
      
      

      },

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

  code_repositories.associate = (db) => {

    db.code_repositories.belongsToMany(db.code_files, {
      as: 'files',
      foreignKey: {
        name: 'code_repositories_filesId',
      },
      constraints: false,
      through: 'code_repositoriesFilesCode_files',
    });

    db.code_repositories.belongsToMany(db.code_files, {
      as: 'files_filter',
      foreignKey: {
        name: 'code_repositories_filesId',
      },
      constraints: false,
      through: 'code_repositoriesFilesCode_files',
    });


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


















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












//end loop



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

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




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

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



  return code_repositories;
};


