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,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

plan_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"pro",


"team",


"enterprise"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived"

        ],

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

billing_period_ends_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.workspace_memberships, {
      as: 'memberships',
      foreignKey: {
        name: 'workspaces_membershipsId',
      },
      constraints: false,
      through: 'workspacesMembershipsWorkspace_memberships',
    });

    db.workspaces.belongsToMany(db.workspace_memberships, {
      as: 'memberships_filter',
      foreignKey: {
        name: 'workspaces_membershipsId',
      },
      constraints: false,
      through: 'workspacesMembershipsWorkspace_memberships',
    });


/// 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.workspace_memberships, {
      as: 'workspace_memberships_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


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














//end loop



    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;
};


