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

title: {
        type: DataTypes.TEXT,
      
      

      },

content_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"promo_copy",


"blog",


"landing_page",


"ad_creative",


"email_campaign",


"social_post",


"design_brief",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"review",


"approved",


"scheduled",


"published",


"archived"

        ],

      },

body: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

scheduled_at: {
        type: DataTypes.DATE,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

channel: {
        type: DataTypes.TEXT,
      
      

      },

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

  content_items.associate = (db) => {

    db.content_items.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'content_items_tagsId',
      },
      constraints: false,
      through: 'content_itemsTagsTags',
    });

    db.content_items.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'content_items_tagsId',
      },
      constraints: false,
      through: 'content_itemsTagsTags',
    });


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
































//end loop



    db.content_items.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.content_items.belongsTo(db.business_units, {
      as: 'business_unit',
      foreignKey: {
        name: 'business_unitId',
      },
      constraints: false,
    });

    db.content_items.belongsTo(db.content_folders, {
      as: 'folder',
      foreignKey: {
        name: 'folderId',
      },
      constraints: false,
    });

    db.content_items.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return content_items;
};


