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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


    db.organizations.hasMany(db.notifications, {
      as: 'notifications_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.imports, {
      as: 'imports_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });



//end loop






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

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



  return organizations;
};


