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: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

cta_preference: {
        type: DataTypes.ENUM,
      
      

        values: [

"whatsapp",


"phone",


"quote_form"

        ],

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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.faqs, {
      as: 'faqs_service',
      foreignKey: {
          name: 'serviceId',
      },
      constraints: false,
    });


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


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




//end loop



    db.services.belongsTo(db.service_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });



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


