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

name: {
        type: DataTypes.TEXT,
      
      

      },

known_for_department: {
        type: DataTypes.TEXT,
      
      

      },

birth_date: {
        type: DataTypes.DATE,
      
      

      },

death_date: {
        type: DataTypes.DATE,
      
      

      },

biography: {
        type: DataTypes.TEXT,
      
      

      },

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

  people.associate = (db) => {


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









    db.people.hasMany(db.title_cast, {
      as: 'title_cast_person',
      foreignKey: {
          name: 'personId',
      },
      constraints: false,
    });


    db.people.hasMany(db.title_crew, {
      as: 'title_crew_person',
      foreignKey: {
          name: 'personId',
      },
      constraints: false,
    });









//end loop





    db.people.hasMany(db.file, {
      as: 'profile_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.people.getTableName(),
        belongsToColumn: 'profile_images',
      },
    });


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

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



  return people;
};


