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

title: {
        type: DataTypes.TEXT,
      
      

      },

publisher: {
        type: DataTypes.TEXT,
      
      

      },

edition: {
        type: DataTypes.TEXT,
      
      

      },

isbn: {
        type: DataTypes.TEXT,
      
      

      },

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

  textbooks.associate = (db) => {


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








    db.textbooks.hasMany(db.chapters, {
      as: 'chapters_textbook',
      foreignKey: {
          name: 'textbookId',
      },
      constraints: false,
    });




    db.textbooks.hasMany(db.question_banks, {
      as: 'question_banks_textbook',
      foreignKey: {
          name: 'textbookId',
      },
      constraints: false,
    });




    db.textbooks.hasMany(db.tests, {
      as: 'tests_textbook',
      foreignKey: {
          name: 'textbookId',
      },
      constraints: false,
    });















//end loop



    db.textbooks.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.textbooks.belongsTo(db.grade_levels, {
      as: 'grade_level',
      foreignKey: {
        name: 'grade_levelId',
      },
      constraints: false,
    });



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


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

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



  return textbooks;
};


