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

title: {
        type: DataTypes.TEXT,
      
      

      },

subtitle: {
        type: DataTypes.TEXT,
      
      

      },

isbn: {
        type: DataTypes.TEXT,
      
      

      },

language: {
        type: DataTypes.ENUM,
      
      

        values: [

"uz",


"ru",


"en",


"other"

        ],

      },

format: {
        type: DataTypes.ENUM,
      
      

        values: [

"ebook",


"audiobook",


"videobook",


"physical_reference"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

page_count: {
        type: DataTypes.INTEGER,
      
      

      },

word_count: {
        type: DataTypes.INTEGER,
      
      

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

difficulty_score: {
        type: DataTypes.DECIMAL,
      
      

      },

popularity_score: {
        type: DataTypes.DECIMAL,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  books.associate = (db) => {

    db.books.belongsToMany(db.authors, {
      as: 'authors',
      foreignKey: {
        name: 'books_authorsId',
      },
      constraints: false,
      through: 'booksAuthorsAuthors',
    });

    db.books.belongsToMany(db.authors, {
      as: 'authors_filter',
      foreignKey: {
        name: 'books_authorsId',
      },
      constraints: false,
      through: 'booksAuthorsAuthors',
    });

    db.books.belongsToMany(db.genres, {
      as: 'genres',
      foreignKey: {
        name: 'books_genresId',
      },
      constraints: false,
      through: 'booksGenresGenres',
    });

    db.books.belongsToMany(db.genres, {
      as: 'genres_filter',
      foreignKey: {
        name: 'books_genresId',
      },
      constraints: false,
      through: 'booksGenresGenres',
    });


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









    db.books.hasMany(db.user_library_items, {
      as: 'user_library_items_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });


    db.books.hasMany(db.bookmarks, {
      as: 'bookmarks_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });


    db.books.hasMany(db.notes, {
      as: 'notes_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });


    db.books.hasMany(db.camera_scan_sessions, {
      as: 'camera_scan_sessions_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });


    db.books.hasMany(db.coin_ledger_entries, {
      as: 'coin_ledger_entries_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });












    db.books.hasMany(db.quiz_definitions, {
      as: 'quiz_definitions_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });









    db.books.hasMany(db.reading_circle_events, {
      as: 'reading_circle_events_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });


    db.books.hasMany(db.recommendation_events, {
      as: 'recommendation_events_book',
      foreignKey: {
          name: 'bookId',
      },
      constraints: false,
    });







//end loop



    db.books.belongsTo(db.publishers, {
      as: 'publisher',
      foreignKey: {
        name: 'publisherId',
      },
      constraints: false,
    });



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

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


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

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



  return books;
};


