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

front: {
        type: DataTypes.TEXT,
      
      

      },

back: {
        type: DataTypes.TEXT,
      
      

      },

hint: {
        type: DataTypes.TEXT,
      
      

      },

card_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"basic",


"cloze",


"image_occlusion"

        ],

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

is_suspended: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  flashcards.associate = (db) => {


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





















    db.flashcards.hasMany(db.spaced_repetition_states, {
      as: 'spaced_repetition_states_flashcard',
      foreignKey: {
          name: 'flashcardId',
      },
      constraints: false,
    });



    db.flashcards.hasMany(db.review_events, {
      as: 'review_events_flashcard',
      foreignKey: {
          name: 'flashcardId',
      },
      constraints: false,
    });











//end loop



    db.flashcards.belongsTo(db.flashcard_decks, {
      as: 'deck',
      foreignKey: {
        name: 'deckId',
      },
      constraints: false,
    });

    db.flashcards.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return flashcards;
};


