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

name_ar: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

short_description_ar: {
        type: DataTypes.TEXT,
      
      

      },

description_ar: {
        type: DataTypes.TEXT,
      
      

      },

density: {
        type: DataTypes.DECIMAL,
      
      

      },

water_absorption: {
        type: DataTypes.DECIMAL,
      
      

      },

compressive_strength: {
        type: DataTypes.DECIMAL,
      
      

      },

flexural_strength: {
        type: DataTypes.DECIMAL,
      
      

      },

origin_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"local",


"imported",


"mixed"

        ],

      },

origin_country: {
        type: DataTypes.TEXT,
      
      

      },

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

  stones.associate = (db) => {

    db.stones.belongsToMany(db.finishes, {
      as: 'finishes',
      foreignKey: {
        name: 'stones_finishesId',
      },
      constraints: false,
      through: 'stonesFinishesFinishes',
    });

    db.stones.belongsToMany(db.finishes, {
      as: 'finishes_filter',
      foreignKey: {
        name: 'stones_finishesId',
      },
      constraints: false,
      through: 'stonesFinishesFinishes',
    });

    db.stones.belongsToMany(db.applications, {
      as: 'applications',
      foreignKey: {
        name: 'stones_applicationsId',
      },
      constraints: false,
      through: 'stonesApplicationsApplications',
    });

    db.stones.belongsToMany(db.applications, {
      as: 'applications_filter',
      foreignKey: {
        name: 'stones_applicationsId',
      },
      constraints: false,
      through: 'stonesApplicationsApplications',
    });


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














    db.stones.hasMany(db.stone_variants, {
      as: 'stone_variants_stone',
      foreignKey: {
          name: 'stoneId',
      },
      constraints: false,
    });


    db.stones.hasMany(db.stone_3d_assets, {
      as: 'stone_3d_assets_stone',
      foreignKey: {
          name: 'stoneId',
      },
      constraints: false,
    });










    db.stones.hasMany(db.lead_requests, {
      as: 'lead_requests_interested_stone',
      foreignKey: {
          name: 'interested_stoneId',
      },
      constraints: false,
    });







//end loop



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

    db.stones.belongsTo(db.stone_colors, {
      as: 'color',
      foreignKey: {
        name: 'colorId',
      },
      constraints: false,
    });



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

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


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

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



  return stones;
};


