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

preview_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"web",


"api_docs",


"storybook",


"static"

        ],

      },

preview_url: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"building",


"ready",


"failed",


"stopped"

        ],

      },

built_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

build_log: {
        type: DataTypes.TEXT,
      
      

      },

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

  previews.associate = (db) => {


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



























//end loop



    db.previews.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.previews.belongsTo(db.users, {
      as: 'created_by',
      foreignKey: {
        name: 'created_byId',
      },
      constraints: false,
    });

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




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

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



  return previews;
};


