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

media_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"photo",


"video"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

caption: {
        type: DataTypes.TEXT,
      
      

      },

captured_at: {
        type: DataTypes.DATE,
      
      

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

width: {
        type: DataTypes.INTEGER,
      
      

      },

height: {
        type: DataTypes.INTEGER,
      
      

      },

duration_seconds: {
        type: DataTypes.DECIMAL,
      
      

      },

camera_make: {
        type: DataTypes.TEXT,
      
      

      },

camera_model: {
        type: DataTypes.TEXT,
      
      

      },

gps_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

gps_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"visible",


"hidden",


"flagged",


"deleted"

        ],

      },

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

      },

duplicate_group_key: {
        type: DataTypes.TEXT,
      
      

      },

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

  media_items.associate = (db) => {

    db.media_items.belongsToMany(db.media_ai_tags, {
      as: 'ai_tags',
      foreignKey: {
        name: 'media_items_ai_tagsId',
      },
      constraints: false,
      through: 'media_itemsAi_tagsMedia_ai_tags',
    });

    db.media_items.belongsToMany(db.media_ai_tags, {
      as: 'ai_tags_filter',
      foreignKey: {
        name: 'media_items_ai_tagsId',
      },
      constraints: false,
      through: 'media_itemsAi_tagsMedia_ai_tags',
    });

    db.media_items.belongsToMany(db.media_user_tags, {
      as: 'user_tags',
      foreignKey: {
        name: 'media_items_user_tagsId',
      },
      constraints: false,
      through: 'media_itemsUser_tagsMedia_user_tags',
    });

    db.media_items.belongsToMany(db.media_user_tags, {
      as: 'user_tags_filter',
      foreignKey: {
        name: 'media_items_user_tagsId',
      },
      constraints: false,
      through: 'media_itemsUser_tagsMedia_user_tags',
    });

    db.media_items.belongsToMany(db.likes, {
      as: 'likes',
      foreignKey: {
        name: 'media_items_likesId',
      },
      constraints: false,
      through: 'media_itemsLikesLikes',
    });

    db.media_items.belongsToMany(db.likes, {
      as: 'likes_filter',
      foreignKey: {
        name: 'media_items_likesId',
      },
      constraints: false,
      through: 'media_itemsLikesLikes',
    });

    db.media_items.belongsToMany(db.comments, {
      as: 'comments',
      foreignKey: {
        name: 'media_items_commentsId',
      },
      constraints: false,
      through: 'media_itemsCommentsComments',
    });

    db.media_items.belongsToMany(db.comments, {
      as: 'comments_filter',
      foreignKey: {
        name: 'media_items_commentsId',
      },
      constraints: false,
      through: 'media_itemsCommentsComments',
    });

    db.media_items.belongsToMany(db.favourites, {
      as: 'saves',
      foreignKey: {
        name: 'media_items_savesId',
      },
      constraints: false,
      through: 'media_itemsSavesFavourites',
    });

    db.media_items.belongsToMany(db.favourites, {
      as: 'saves_filter',
      foreignKey: {
        name: 'media_items_savesId',
      },
      constraints: false,
      through: 'media_itemsSavesFavourites',
    });

    db.media_items.belongsToMany(db.mentions, {
      as: 'mentions',
      foreignKey: {
        name: 'media_items_mentionsId',
      },
      constraints: false,
      through: 'media_itemsMentionsMentions',
    });

    db.media_items.belongsToMany(db.mentions, {
      as: 'mentions_filter',
      foreignKey: {
        name: 'media_items_mentionsId',
      },
      constraints: false,
      through: 'media_itemsMentionsMentions',
    });

    db.media_items.belongsToMany(db.face_detections, {
      as: 'face_detections',
      foreignKey: {
        name: 'media_items_face_detectionsId',
      },
      constraints: false,
      through: 'media_itemsFace_detectionsFace_detections',
    });

    db.media_items.belongsToMany(db.face_detections, {
      as: 'face_detections_filter',
      foreignKey: {
        name: 'media_items_face_detectionsId',
      },
      constraints: false,
      through: 'media_itemsFace_detectionsFace_detections',
    });

    db.media_items.belongsToMany(db.download_jobs, {
      as: 'watermarked_downloads',
      foreignKey: {
        name: 'media_items_watermarked_downloadsId',
      },
      constraints: false,
      through: 'media_itemsWatermarked_downloadsDownload_jobs',
    });

    db.media_items.belongsToMany(db.download_jobs, {
      as: 'watermarked_downloads_filter',
      foreignKey: {
        name: 'media_items_watermarked_downloadsId',
      },
      constraints: false,
      through: 'media_itemsWatermarked_downloadsDownload_jobs',
    });


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
















    db.media_items.hasMany(db.upload_files, {
      as: 'upload_files_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });



    db.media_items.hasMany(db.media_ai_tags, {
      as: 'media_ai_tags_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.media_user_tags, {
      as: 'media_user_tags_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.likes, {
      as: 'likes_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.favourites, {
      as: 'favourites_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.comments, {
      as: 'comments_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.mentions, {
      as: 'mentions_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.notifications, {
      as: 'notifications_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });




    db.media_items.hasMany(db.download_jobs, {
      as: 'download_jobs_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });




    db.media_items.hasMany(db.ai_embeddings, {
      as: 'ai_embeddings_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.face_detections, {
      as: 'face_detections_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });




    db.media_items.hasMany(db.ai_captions, {
      as: 'ai_captions_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.moderation_flags, {
      as: 'moderation_flags_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });



    db.media_items.hasMany(db.story_items, {
      as: 'story_items_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });


    db.media_items.hasMany(db.analytics_events, {
      as: 'analytics_events_media_item',
      foreignKey: {
          name: 'media_itemId',
      },
      constraints: false,
    });




//end loop



    db.media_items.belongsTo(db.albums, {
      as: 'album',
      foreignKey: {
        name: 'albumId',
      },
      constraints: false,
    });

    db.media_items.belongsTo(db.users, {
      as: 'uploader',
      foreignKey: {
        name: 'uploaderId',
      },
      constraints: false,
    });

    db.media_items.belongsTo(db.storage_objects, {
      as: 'storage_object',
      foreignKey: {
        name: 'storage_objectId',
      },
      constraints: false,
    });

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




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

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



  return media_items;
};


