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

title: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

location_text: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"in_progress",


"completed"

        ],

      },

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

      },

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

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

  projects.associate = (db) => {

    db.projects.belongsToMany(db.services, {
      as: 'services',
      foreignKey: {
        name: 'projects_servicesId',
      },
      constraints: false,
      through: 'projectsServicesServices',
    });

    db.projects.belongsToMany(db.services, {
      as: 'services_filter',
      foreignKey: {
        name: 'projects_servicesId',
      },
      constraints: false,
      through: 'projectsServicesServices',
    });


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










    db.projects.hasMany(db.testimonials, {
      as: 'testimonials_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });



    db.projects.hasMany(db.leads, {
      as: 'leads_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.media_assets, {
      as: 'media_assets_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });




//end loop





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

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

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


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

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



  return projects;
};


