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,
      
      

      },

workspace_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"personal",


"organization"

        ],

      },

theme_preset: {
        type: DataTypes.ENUM,
      
      

        values: [

"y2k_glass",


"holographic",


"minimal_dark",


"neon_cyan",


"electric_blue"

        ],

      },

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



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



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



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



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


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


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


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




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



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




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



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



    db.workspaces.hasMany(db.dashboard_snapshots, {
      as: 'dashboard_snapshots_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_image',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.workspaces.getTableName(),
        belongsToColumn: 'logo_image',
      },
    });


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

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



  return workspaces;
};


