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

title: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

portfolio_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"image",


"video",


"document",


"link",


"mixed"

        ],

      },

external_url: {
        type: DataTypes.TEXT,
      
      

      },

client_testimonial: {
        type: DataTypes.TEXT,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  portfolios.associate = (db) => {

    db.portfolios.belongsToMany(db.skills, {
      as: 'related_skills',
      foreignKey: {
        name: 'portfolios_related_skillsId',
      },
      constraints: false,
      through: 'portfoliosRelated_skillsSkills',
    });

    db.portfolios.belongsToMany(db.skills, {
      as: 'related_skills_filter',
      foreignKey: {
        name: 'portfolios_related_skillsId',
      },
      constraints: false,
      through: 'portfoliosRelated_skillsSkills',
    });


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




























//end loop



    db.portfolios.belongsTo(db.users, {
      as: 'freelancer',
      foreignKey: {
        name: 'freelancerId',
      },
      constraints: false,
    });



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

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


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

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



  return portfolios;
};


