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

name: {
        type: DataTypes.TEXT,
      
      

      },

nama: {
        type: DataTypes.STRING(255),
      
      

      },

kategori: {
        type: DataTypes.STRING(100),
      
      

      },

subkategori: {
        type: DataTypes.STRING(100),
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

full_description: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

alamat: {
        type: DataTypes.TEXT,
      
      

      },

kelurahan: {
        type: DataTypes.STRING(100),
      
      

      },

kecamatan: {
        type: DataTypes.STRING(100),
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

kota: {
        type: DataTypes.STRING(100),
      
      

      },

province: {
        type: DataTypes.TEXT,
      
      

      },

provinsi: {
        type: DataTypes.STRING(100),
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

kode_pos: {
        type: DataTypes.STRING(20),
      
      

      },

latitude: {
        type: DataTypes.DOUBLE,
      
      

      },

longitude: {
        type: DataTypes.DOUBLE,
      
      

      },

phone_number: {
        type: DataTypes.TEXT,
      
      

      },

telepon: {
        type: DataTypes.STRING(100),
      
      

      },

whatsapp_number: {
        type: DataTypes.TEXT,
      
      

      },

whatsapp: {
        type: DataTypes.STRING(100),
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

website_url: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.STRING(255),
      
      

      },

google_maps_url: {
        type: DataTypes.TEXT,
      
      

      },

jam_buka: {
        type: DataTypes.STRING(255),
      
      

      },

price_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"budget",


"midrange",


"premium",


"unknown"

        ],

      },

average_price: {
        type: DataTypes.DECIMAL,
      
      

      },

rating_average: {
        type: DataTypes.DECIMAL,
      
      

      },

rating: {
        type: DataTypes.DECIMAL(2, 1),
        defaultValue: 0,
      
      

      },

rating_count: {
        type: DataTypes.INTEGER,
      
      

      },

review_count: {
        type: DataTypes.INTEGER,
        defaultValue: 0,
      
      

      },

status: {
        type: DataTypes.STRING(50),
        defaultValue: 'aktif',
      
      

      },

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

      },

verified: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
      
      

      },

featured: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
      
      

      },

popularitas: {
        type: DataTypes.INTEGER,
        defaultValue: 0,
      
      

      },

created_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
      
      

      },

updated_at: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
      
      

      },

sumber: {
        type: DataTypes.STRING(100),
      
      

      },

external_id: {
        type: DataTypes.STRING(255),
      
      

      },

external_type: {
        type: DataTypes.STRING(50),
      
      

      },

raw_source_data: {
        type: DataTypes.JSONB,
      
      

      },

last_synced_at: {
        type: DataTypes.DATE,
      
      

      },

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


