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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

pricing_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed",


"hourly"

        ],

      },

starting_price: {
        type: DataTypes.DECIMAL,
      
      

      },

delivery_days: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"paused"

        ],

      },

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

      },

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

  service_listings.associate = (db) => {

    db.service_listings.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'service_listings_tagsId',
      },
      constraints: false,
      through: 'service_listingsTagsTags',
    });

    db.service_listings.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'service_listings_tagsId',
      },
      constraints: false,
      through: 'service_listingsTagsTags',
    });

    db.service_listings.belongsToMany(db.skills, {
      as: 'included_skills',
      foreignKey: {
        name: 'service_listings_included_skillsId',
      },
      constraints: false,
      through: 'service_listingsIncluded_skillsSkills',
    });

    db.service_listings.belongsToMany(db.skills, {
      as: 'included_skills_filter',
      foreignKey: {
        name: 'service_listings_included_skillsId',
      },
      constraints: false,
      through: 'service_listingsIncluded_skillsSkills',
    });


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




























//end loop



    db.service_listings.belongsTo(db.users, {
      as: 'freelancer',
      foreignKey: {
        name: 'freelancerId',
      },
      constraints: false,
    });

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



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

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


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

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



  return service_listings;
};


