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.role_permissions, {
      as: 'role_permissions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.memberships, {
      as: 'memberships_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.events, {
      as: 'events_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


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


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


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


    db.organizations.hasMany(db.storage_buckets, {
      as: 'storage_buckets_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


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


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


    db.organizations.hasMany(db.tags, {
      as: 'tags_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


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


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


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


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


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


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


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


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


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


    db.organizations.hasMany(db.watermark_profiles, {
      as: 'watermark_profiles_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


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


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


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


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


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


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


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


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


    db.organizations.hasMany(db.analytics_events, {
      as: 'analytics_events_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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



//end loop






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

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



  return organizations;
};


