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

title_ar: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

excerpt_ar: {
        type: DataTypes.TEXT,
      
      

      },

content_ar: {
        type: DataTypes.TEXT,
      
      

      },

publish_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"scheduled",


"published",


"archived"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  blog_posts.associate = (db) => {

    db.blog_posts.belongsToMany(db.stones, {
      as: 'related_stones',
      foreignKey: {
        name: 'blog_posts_related_stonesId',
      },
      constraints: false,
      through: 'blog_postsRelated_stonesStones',
    });

    db.blog_posts.belongsToMany(db.stones, {
      as: 'related_stones_filter',
      foreignKey: {
        name: 'blog_posts_related_stonesId',
      },
      constraints: false,
      through: 'blog_postsRelated_stonesStones',
    });

    db.blog_posts.belongsToMany(db.projects, {
      as: 'related_projects',
      foreignKey: {
        name: 'blog_posts_related_projectsId',
      },
      constraints: false,
      through: 'blog_postsRelated_projectsProjects',
    });

    db.blog_posts.belongsToMany(db.projects, {
      as: 'related_projects_filter',
      foreignKey: {
        name: 'blog_posts_related_projectsId',
      },
      constraints: false,
      through: 'blog_postsRelated_projectsProjects',
    });


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






























//end loop



    db.blog_posts.belongsTo(db.blog_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

    db.blog_posts.belongsTo(db.users, {
      as: 'author',
      foreignKey: {
        name: 'authorId',
      },
      constraints: false,
    });



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

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


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

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



  return blog_posts;
};


