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,
      
      

      },

plan: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"creator",


"pro",


"studio",


"enterprise"

        ],

      },

plan_started_at: {
        type: DataTypes.DATE,
      
      

      },

plan_ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  workspaces.associate = (db) => {


/// 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,
    });





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





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








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




//end loop



    db.workspaces.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      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;
};


