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_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"pro",


"team",


"enterprise"

        ],

      },

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




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


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


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



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




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


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


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


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



//end loop



    db.workspaces.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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



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


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

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



  return workspaces;
};


