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

name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"paused",


"archived"

        ],

      },

last_opened_at: {
        type: DataTypes.DATE,
      
      

      },

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

  workspaces.associate = (db) => {

    db.workspaces.belongsToMany(db.chats, {
      as: 'chats',
      foreignKey: {
        name: 'workspaces_chatsId',
      },
      constraints: false,
      through: 'workspacesChatsChats',
    });

    db.workspaces.belongsToMany(db.chats, {
      as: 'chats_filter',
      foreignKey: {
        name: 'workspaces_chatsId',
      },
      constraints: false,
      through: 'workspacesChatsChats',
    });

    db.workspaces.belongsToMany(db.build_jobs, {
      as: 'build_jobs',
      foreignKey: {
        name: 'workspaces_build_jobsId',
      },
      constraints: false,
      through: 'workspacesBuild_jobsBuild_jobs',
    });

    db.workspaces.belongsToMany(db.build_jobs, {
      as: 'build_jobs_filter',
      foreignKey: {
        name: 'workspaces_build_jobsId',
      },
      constraints: false,
      through: 'workspacesBuild_jobsBuild_jobs',
    });


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














    db.workspaces.hasMany(db.chats, {
      as: 'chats_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });




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



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



    db.workspaces.hasMany(db.build_jobs, {
      as: 'build_jobs_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });




    db.workspaces.hasMany(db.web_search_queries, {
      as: 'web_search_queries_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });






//end loop



    db.workspaces.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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




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

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



  return workspaces;
};


