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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  organizations.associate = (db) => {


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


    db.organizations.hasMany(db.users, {
      as: 'users_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });





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


    db.organizations.hasMany(db.workspace_memberships, {
      as: 'workspace_memberships_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.genres, {
      as: 'genres_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.languages, {
      as: 'languages_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ai_models, {
      as: 'ai_models_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.projects, {
      as: 'projects_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.project_collaborations, {
      as: 'project_collaborations_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.songs, {
      as: 'songs_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.song_metadata, {
      as: 'song_metadata_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.assets, {
      as: 'assets_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.recording_sessions, {
      as: 'recording_sessions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.tracks, {
      as: 'tracks_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.arrangement_sections, {
      as: 'arrangement_sections_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.generation_requests, {
      as: 'generation_requests_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.vocal_processing_presets, {
      as: 'vocal_processing_presets_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.mastering_presets, {
      as: 'mastering_presets_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.mix_sessions, {
      as: 'mix_sessions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.mastering_sessions, {
      as: 'mastering_sessions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.exports, {
      as: 'exports_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.cover_artworks, {
      as: 'cover_artworks_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.marketplace_orders, {
      as: 'marketplace_orders_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });



//end loop






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

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



  return organizations;
};


