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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    //end loop

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

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

  return organizations;
};
