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,
      },

name_ar: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

project_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"interior",


"exterior",


"commercial",


"residential"

        ],

      },

location_ar: {
        type: DataTypes.TEXT,
      
      

      },

summary_ar: {
        type: DataTypes.TEXT,
      
      

      },

description_ar: {
        type: DataTypes.TEXT,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

      },

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

  projects.associate = (db) => {

    db.projects.belongsToMany(db.stones, {
      as: 'stones_used',
      foreignKey: {
        name: 'projects_stones_usedId',
      },
      constraints: false,
      through: 'projectsStones_usedStones',
    });

    db.projects.belongsToMany(db.stones, {
      as: 'stones_used_filter',
      foreignKey: {
        name: 'projects_stones_usedId',
      },
      constraints: false,
      through: 'projectsStones_usedStones',
    });

    db.projects.belongsToMany(db.applications, {
      as: 'applications',
      foreignKey: {
        name: 'projects_applicationsId',
      },
      constraints: false,
      through: 'projectsApplicationsApplications',
    });

    db.projects.belongsToMany(db.applications, {
      as: 'applications_filter',
      foreignKey: {
        name: 'projects_applicationsId',
      },
      constraints: false,
      through: 'projectsApplicationsApplications',
    });


/// 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.project_media, {
      as: 'project_media_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });





    db.projects.hasMany(db.lead_requests, {
      as: 'lead_requests_related_project',
      foreignKey: {
          name: 'related_projectId',
      },
      constraints: false,
    });







//end loop





    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.hasMany(db.file, {
      as: 'gallery_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.projects.getTableName(),
        belongsToColumn: 'gallery_images',
      },
    });

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


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

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



  return projects;
};


