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

name_ar: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

service_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"cnc_cutting",


"waterjet_cutting",


"polishing",


"profiling",


"edge_finishing",


"engraving",


"installation_support",


"custom_fabrication",


"consultation",


"other"

        ],

      },

summary_ar: {
        type: DataTypes.TEXT,
      
      

      },

description_ar: {
        type: DataTypes.TEXT,
      
      

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

      },

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

  services.associate = (db) => {


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


















    db.services.hasMany(db.service_technologies, {
      as: 'service_technologies_service',
      foreignKey: {
          name: 'serviceId',
      },
      constraints: false,
    });







    db.services.hasMany(db.lead_requests, {
      as: 'lead_requests_interested_service',
      foreignKey: {
          name: 'interested_serviceId',
      },
      constraints: false,
    });







//end loop





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

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


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

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



  return services;
};


