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.role_assignments, {
      as: 'role_assignments_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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



//end loop






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

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



  return organizations;
};


