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

title_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"movie",


"tv_show"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

original_name: {
        type: DataTypes.TEXT,
      
      

      },

tagline: {
        type: DataTypes.TEXT,
      
      

      },

overview: {
        type: DataTypes.TEXT,
      
      

      },

release_date: {
        type: DataTypes.DATE,
      
      

      },

first_air_date: {
        type: DataTypes.DATE,
      
      

      },

last_air_date: {
        type: DataTypes.DATE,
      
      

      },

content_rating: {
        type: DataTypes.ENUM,
      
      

        values: [

"g",


"pg",


"pg_13",


"r",


"nc_17",


"tv_y",


"tv_y7",


"tv_g",


"tv_pg",


"tv_14",


"tv_ma",


"unrated"

        ],

      },

original_language: {
        type: DataTypes.TEXT,
      
      

      },

runtime_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

number_of_seasons: {
        type: DataTypes.INTEGER,
      
      

      },

number_of_episodes: {
        type: DataTypes.INTEGER,
      
      

      },

average_rating: {
        type: DataTypes.DECIMAL,
      
      

      },

rating_count: {
        type: DataTypes.INTEGER,
      
      

      },

popularity_score: {
        type: DataTypes.DECIMAL,
      
      

      },

imdb_code: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  titles.associate = (db) => {


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







    db.titles.hasMany(db.title_genres, {
      as: 'title_genres_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });



    db.titles.hasMany(db.title_cast, {
      as: 'title_cast_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });


    db.titles.hasMany(db.title_crew, {
      as: 'title_crew_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });


    db.titles.hasMany(db.videos, {
      as: 'videos_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });



    db.titles.hasMany(db.watchlist_items, {
      as: 'watchlist_items_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });


    db.titles.hasMany(db.user_ratings, {
      as: 'user_ratings_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });


    db.titles.hasMany(db.recommendations, {
      as: 'recommendations_source_title',
      foreignKey: {
          name: 'source_titleId',
      },
      constraints: false,
    });

    db.titles.hasMany(db.recommendations, {
      as: 'recommendations_recommended_title',
      foreignKey: {
          name: 'recommended_titleId',
      },
      constraints: false,
    });


    db.titles.hasMany(db.trending_entries, {
      as: 'trending_entries_title',
      foreignKey: {
          name: 'titleId',
      },
      constraints: false,
    });



//end loop





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

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


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

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



  return titles;
};


