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

name: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state_region: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

access_instructions: {
        type: DataTypes.TEXT,
      
      

      },

bedrooms: {
        type: DataTypes.INTEGER,
      
      

      },

bathrooms: {
        type: DataTypes.INTEGER,
      
      

      },

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

  properties.associate = (db) => {


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












    db.properties.hasMany(db.jobs, {
      as: 'jobs_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });











    db.properties.hasMany(db.estimates, {
      as: 'estimates_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });



    db.properties.hasMany(db.recurring_schedules, {
      as: 'recurring_schedules_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });







//end loop



    db.properties.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.properties.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.properties.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return properties;
};


