Manos Chorianopoulos

dynamic CouponSet categories at MyRewardsVC

...@@ -202,8 +202,7 @@ import UIKit ...@@ -202,8 +202,7 @@ import UIKit
202 self.merchantCategories = categories 202 self.merchantCategories = categories
203 print("✅ [MyRewardsViewController] Loaded \(categories.count) merchant categories") 203 print("✅ [MyRewardsViewController] Loaded \(categories.count) merchant categories")
204 204
205 - // TODO: Implement category-based filtering for coupon sets sections 205 + // Create coupon sets sections with category-based filtering
206 - // For now, create the standard coupon sets section
207 self.createCouponSetsSection() 206 self.createCouponSetsSection()
208 207
209 } failureCallback: { [weak self] errorCode in 208 } failureCallback: { [weak self] errorCode in
...@@ -214,44 +213,131 @@ import UIKit ...@@ -214,44 +213,131 @@ import UIKit
214 } 213 }
215 214
216 private func createCouponSetsSection() { 215 private func createCouponSetsSection() {
217 - // TODO: IMPLEMENT CATEGORY-BASED FILTERING 216 + print("🔍 [MyRewardsViewController] Starting coupon filtering:")
218 - // 217 + print(" - Coupon Sets: \(couponSets.count)")
219 - // Current logic: Creates one section with all coupon sets 218 + print(" - Merchants: \(merchants.count)")
220 - // 219 + print(" - Categories: \(merchantCategories.count)")
221 - // Future enhancement: Filter coupon sets into different sections based on categories
222 - // Logic:
223 - // 1. For each couponset, get its merchant_uuid
224 - // 2. Find the merchant with that merchant_uuid in self.merchants
225 - // 3. Get the merchant's category_uuid
226 - // 4. Find the category with that category_uuid in self.merchantCategories
227 - // 5. Group coupon sets by category
228 - // 6. Create separate sections for each category
229 - //
230 - // Example structure after filtering:
231 - // - Section: "Εκπαίδευση" (Education) - coupon sets from education merchants
232 - // - Section: "Ψυχαγωγία" (Entertainment) - coupon sets from entertainment merchants
233 - // - etc.
234 - //
235 - // Implementation steps:
236 - // 1. Create a dictionary to group coupon sets by category: [String: [CouponSetItemModel]]
237 - // 2. Iterate through self.couponSets
238 - // 3. For each coupon set, find its merchant and category
239 - // 4. Add coupon set to the appropriate category group
240 - // 5. Create a SectionModel for each category group
241 - // 6. Sort sections by category name or priority
242 220
243 - // Current implementation (temporary): 221 + // Check if we have all required data for filtering
222 + guard !couponSets.isEmpty, !merchants.isEmpty, !merchantCategories.isEmpty else {
223 + print("⚠️ [MyRewardsViewController] Missing data for filtering - using fallback single section")
224 + print(" - Coupon Sets Empty: \(couponSets.isEmpty)")
225 + print(" - Merchants Empty: \(merchants.isEmpty)")
226 + print(" - Categories Empty: \(merchantCategories.isEmpty)")
227 +
228 + // Fallback: Create single section with all coupon sets
229 + createSingleCouponSetsSection()
230 + return
231 + }
232 +
233 + // Group coupon sets by merchant category
234 + var categorySections: [SectionModel] = []
235 + var processedCouponSets: Set<String> = [] // Track processed coupon sets to avoid duplicates
236 +
237 + print("🔄 [MyRewardsViewController] Processing categories for filtering...")
238 +
239 + for category in merchantCategories {
240 + // Find merchants in this category
241 + let categoryMerchants = merchants.filter { merchant in
242 + merchant._category_uuid == category._uuid
243 + }
244 +
245 + print(" - Category '\(category.displayName)' has \(categoryMerchants.count) merchants")
246 +
247 + // Find coupon sets from merchants in this category
248 + let categoryCouponSets = couponSets.filter { couponSet in
249 + // Skip if already processed (avoid duplicates)
250 + guard !processedCouponSets.contains(couponSet._uuid) else { return false }
251 +
252 + // Check if this coupon set belongs to any merchant in this category
253 + let belongsToCategory = categoryMerchants.contains { merchant in
254 + merchant._uuid == couponSet._merchant_uuid
255 + }
256 +
257 + if belongsToCategory {
258 + processedCouponSets.insert(couponSet._uuid)
259 + }
260 +
261 + return belongsToCategory
262 + }
263 +
264 + print(" - Category '\(category.displayName)' has \(categoryCouponSets.count) coupon sets")
265 +
266 + // Create section if we have coupon sets for this category
267 + if !categoryCouponSets.isEmpty {
268 + let section = SectionModel(
269 + sectionType: .myRewardsHorizontalCouponsets,
270 + title: category.displayName,
271 + items: categoryCouponSets,
272 + itemType: .couponSets
273 + )
274 + categorySections.append(section)
275 +
276 + print(" ✅ Created section for '\(category.displayName)' with \(categoryCouponSets.count) items")
277 + }
278 + }
279 +
280 + // Handle any remaining unmatched coupon sets
281 + let unmatchedCouponSets = couponSets.filter { couponSet in
282 + !processedCouponSets.contains(couponSet._uuid)
283 + }
284 +
285 + if !unmatchedCouponSets.isEmpty {
286 + print(" ⚠️ Found \(unmatchedCouponSets.count) unmatched coupon sets - adding to 'Άλλες Προσφορές' section")
287 +
288 + let unmatchedSection = SectionModel(
289 + sectionType: .myRewardsHorizontalCouponsets,
290 + title: "Άλλες Προσφορές", // "Other Offers"
291 + items: unmatchedCouponSets,
292 + itemType: .couponSets
293 + )
294 + categorySections.append(unmatchedSection)
295 + }
296 +
297 + // Sort sections by title for consistent ordering
298 + categorySections.sort { section1, section2 in
299 + let title1 = section1.title ?? ""
300 + let title2 = section2.title ?? ""
301 +
302 + // Put "Άλλες Προσφορές" at the end
303 + if title1 == "Άλλες Προσφορές" { return false }
304 + if title2 == "Άλλες Προσφορές" { return true }
305 +
306 + return title1.localizedCaseInsensitiveCompare(title2) == .orderedAscending
307 + }
308 +
309 + print("✅ [MyRewardsViewController] Created \(categorySections.count) category sections:")
310 + for section in categorySections {
311 + print(" - '\(section.title ?? "Unknown")': \(section.itemCount) coupon sets")
312 + }
313 +
314 + // Add category sections to main sections array
315 + self.sections.append(contentsOf: categorySections)
316 +
317 + // Reload table view with new sections
318 + DispatchQueue.main.async {
319 + self.tableView.reloadData()
320 + }
321 + }
322 +
323 + private func createSingleCouponSetsSection() {
324 + print("📦 [MyRewardsViewController] Creating single fallback coupon sets section")
325 +
326 + // Fallback: Single section with all coupon sets
244 if !self.couponSets.isEmpty { 327 if !self.couponSets.isEmpty {
245 let couponSetsSection = SectionModel( 328 let couponSetsSection = SectionModel(
246 sectionType: .myRewardsHorizontalCouponsets, 329 sectionType: .myRewardsHorizontalCouponsets,
247 - title: "Προσφορές", 330 + title: "Προσφορές", // "Offers"
248 items: self.couponSets, 331 items: self.couponSets,
249 itemType: .couponSets 332 itemType: .couponSets
250 ) 333 )
251 self.sections.append(couponSetsSection) 334 self.sections.append(couponSetsSection)
335 +
336 + print("✅ [MyRewardsViewController] Created fallback section with \(self.couponSets.count) coupon sets")
337 + } else {
338 + print("⚠️ [MyRewardsViewController] No coupon sets available - no section created")
252 } 339 }
253 340
254 - // Reload table view with new sections
255 DispatchQueue.main.async { 341 DispatchQueue.main.async {
256 self.tableView.reloadData() 342 self.tableView.reloadData()
257 } 343 }
......