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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

zip: {
        type: DataTypes.TEXT,
      
      

      },

lat: {
        type: DataTypes.DECIMAL,
      
      

      },

lng: {
        type: DataTypes.DECIMAL,
      
      

      },

hours_json: {
        type: DataTypes.TEXT,
      
      

      },

availability_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"AVAILABLE_TODAY",


"THIS_WEEK",


"BOOKED_OUT"

        ],

      },

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

      },

reliability_score: {
        type: DataTypes.INTEGER,
      
      

      },

reliability_breakdown_json: {
        type: DataTypes.TEXT,
      
      

      },

response_time_median_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

tenant_key: {
        type: DataTypes.TEXT,
      
      

      },

google_place_id: {
        type: DataTypes.TEXT,
        allowNull: true,
      },

      is_claimed: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        allowNull: false,
      },

      rating: {
        type: DataTypes.DECIMAL(3, 2),
        defaultValue: 0,
        allowNull: false,
      },

      planId: {
        type: DataTypes.UUID,
        allowNull: true,
      },

      renewal_date: {
        type: DataTypes.DATE,
        allowNull: true,
      },

created_at_ts: {
        type: DataTypes.DATE,
      
      

      },

updated_at_ts: {
        type: DataTypes.DATE,
      
      

      },

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

  businesses.associate = (db) => {


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









    db.businesses.hasMany(db.business_photos, {
      as: 'business_photos_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.business_categories, {
      as: 'business_categories_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.service_prices, {
      as: 'service_prices_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.business_badges, {
      as: 'business_badges_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.verification_submissions, {
      as: 'verification_submissions_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });





    db.businesses.hasMany(db.lead_matches, {
      as: 'lead_matches_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });




    db.businesses.hasMany(db.reviews, {
      as: 'reviews_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });





    db.businesses.hasMany(db.trust_adjustments, {
      as: 'trust_adjustments_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });



//end loop



    db.businesses.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });

    db.businesses.belongsTo(db.plans, {
      as: 'plan',
      foreignKey: 'planId',
    });

    db.businesses.hasMany(db.listing_events, {
      as: 'listing_events',
      foreignKey: 'businessId',
    });

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

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



  return businesses;
};
