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

      slug: {
        type: DataTypes.TEXT,
      },

      title: {
        type: DataTypes.TEXT,
      },

      bio: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['DRAFT', 'PUBLISHED', 'SUSPENDED'],
      },

      published_at: {
        type: DataTypes.DATE,
      },

      og_title: {
        type: DataTypes.TEXT,
      },

      og_description: {
        type: DataTypes.TEXT,
      },

      og_image_url: {
        type: DataTypes.TEXT,
      },

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

  profile_pages.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.profile_pages.hasMany(db.analytics_daily, {
      as: 'analytics_daily_profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

    db.profile_pages.hasMany(db.analytics_events, {
      as: 'analytics_events_profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

    db.profile_pages.hasMany(db.blocks, {
      as: 'blocks_profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

    db.profile_pages.hasMany(db.domain_mappings, {
      as: 'domain_mappings_profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

    db.profile_pages.hasMany(db.flags, {
      as: 'flags_profile_page',
      foreignKey: {
        name: 'profile_pageId',
      },
      constraints: false,
    });

    //end loop

    db.profile_pages.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.profile_pages.belongsTo(db.templates, {
      as: 'theme_template',
      foreignKey: {
        name: 'theme_templateId',
      },
      constraints: false,
    });

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

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

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

  return profile_pages;
};
