
module.exports = function(sequelize, DataTypes) {
  const places = sequelize.define(
    'places',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

full_description: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

province: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

phone_number: {
        type: DataTypes.TEXT,
      
      

      },

whatsapp_number: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

website_url: {
        type: DataTypes.TEXT,
      
      

      },

google_maps_url: {
        type: DataTypes.TEXT,
      
      

      },

price_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"budget",


"midrange",


"premium",


"unknown"

        ],

      },

average_price: {
        type: DataTypes.DECIMAL,
      
      

      },

rating_average: {
        type: DataTypes.DECIMAL,
      
      

      },

rating_count: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

      },

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

  places.associate = (db) => {


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







    db.places.hasMany(db.place_opening_hours, {
      as: 'place_opening_hours_place',
      foreignKey: {
          name: 'placeId',
      },
      constraints: false,
    });



    db.places.hasMany(db.place_feature_links, {
      as: 'place_feature_links_place',
      foreignKey: {
          name: 'placeId',
      },
      constraints: false,
    });


    db.places.hasMany(db.place_offerings, {
      as: 'offerings',
      foreignKey: {
        name: 'placeId',
      },
      constraints: false,
    });


    db.places.hasMany(db.reviews, {
      as: 'reviews_place',
      foreignKey: {
          name: 'placeId',
      },
      constraints: false,
    });


    db.places.hasMany(db.favorites, {
      as: 'favorites_place',
      foreignKey: {
          name: 'placeId',
      },
      constraints: false,
    });



    db.places.hasMany(db.reports, {
      as: 'reports_place',
      foreignKey: {
          name: 'placeId',
      },
      constraints: false,
    });



//end loop



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

    db.places.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return places;
};


