Repository Public
1//2// Fabri-Ka iOS v23// File: FabriKa/Core/Models/Store.swift4// Description: boutique — le résumé des listes (aussi gardé sur l'appareil pour les boutiques suivies) et la fiche5// complète avec son catalogue. « Québécois » décrit la boutique, jamais le produit.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import CoreLocation12import Foundation1314struct Certification: Codable, Hashable, Sendable {15 var label: String16 var detail: String?1718 enum CodingKeys: String, CodingKey { case label, detail }19 init(from decoder: Decoder) throws {20 if let s = try? decoder.singleValueContainer().decode(String.self) { label = s; detail = nil; return }21 let c = try decoder.container(keyedBy: CodingKeys.self)22 label = c.text(.label) ?? ""23 detail = c.text(.detail)24 }25}2627/// Résumé d'une boutique. Identifiant = nom de domaine (« bijouteriejodoin.com »).28struct Store: Codable, Hashable, Sendable, Identifiable {29 var id: String30 var name: String31 var city: String?32 var citySlug: String?33 var region: String?34 var regionSlug: String?35 var lat: Double?36 var lng: Double?37 var origin: String?38 var originLabel: String?39 var category: String?40 var categoryLabel: String?41 var n: Int = 042 var nNew: Int = 043 var logo: String?44 var img: String?45 /// jusqu'à trois photos de produits publics de la boutique46 var images: [String] = []47 var platform: String?48 var firstSeen: Double?49 var distanceKm: Double?50 // présents dans le résumé joint à une fiche produit51 var url: String?52 var summary: String?53 var certifications: [Certification] = []54 var verification: String?5556 enum CodingKeys: String, CodingKey {57 case id, name, city, region, lat, lng, origin, category, n, logo, img, images, platform, url, certifications, verification58 case citySlug = "city_slug", regionSlug = "region_slug", originLabel = "origin_label", categoryLabel = "category_label"59 case nNew = "n_new", firstSeen = "first_seen", distanceKm = "distance_km", summary = "description"60 }6162 init(id: String, name: String, city: String? = nil, region: String? = nil, n: Int = 0) {63 self.id = id; self.name = name; self.city = city; self.region = region; self.n = n64 }6566 init(from decoder: Decoder) throws {67 let c = try decoder.container(keyedBy: CodingKeys.self)68 guard let id = c.text(.id) else {69 throw DecodingError.dataCorruptedError(forKey: .id, in: c, debugDescription: "boutique sans identifiant")70 }71 self.id = id72 name = c.text(.name) ?? id73 city = c.text(.city)74 citySlug = c.text(.citySlug)75 region = c.text(.region)76 regionSlug = c.text(.regionSlug)77 lat = c.dbl(.lat)78 lng = c.dbl(.lng)79 origin = c.text(.origin)80 originLabel = c.text(.originLabel)81 category = c.text(.category)82 categoryLabel = c.text(.categoryLabel)83 n = c.int(.n) ?? 084 nNew = c.int(.nNew) ?? 085 logo = c.text(.logo)86 img = c.text(.img)87 images = c.strings(.images)88 platform = c.text(.platform)89 firstSeen = c.dbl(.firstSeen)90 distanceKm = c.dbl(.distanceKm)91 url = c.text(.url)92 summary = c.text(.summary)93 certifications = c.list(.certifications, Certification.self).filter { !$0.label.isEmpty }94 verification = c.text(.verification)95 }9697 var coordinate: CLLocationCoordinate2D? {98 guard let lat, let lng, (44...63).contains(lat), (-80 ... -56).contains(lng) else { return nil }99 return CLLocationCoordinate2D(latitude: lat, longitude: lng)100 }101102 var place: String? { city ?? region }103104 /// Sur-titre de la plaque : « Boutique de Chicoutimi », « Revendeur établi à Gatineau », sinon le fait qu'on ignore.105 var eyebrow: String { Store.eyebrow(origin: origin, city: city, region: region) }106107 static func eyebrow(origin: String?, city: String?, region: String?) -> String {108 let reseller = origin == "revendeur"109 if let city { return reseller ? "Revendeur établi à \(city)" : "Boutique \(Store.de(city))" }110 if let region { return reseller ? "Revendeur · \(region)" : "Boutique · \(region)" }111 return reseller ? "Revendeur du Québec" : "Boutique du Québec"112 }113114 /// « de Chicoutimi », « d'Albanel », « du Plateau Mont-Royal » n'est pas tenté : les noms de secteur restent tels quels.115 static func de(_ place: String) -> String {116 guard let f = place.lowercased().folding(options: .diacriticInsensitive, locale: nil).first else { return "de \(place)" }117 return "aeiouyh".contains(f) ? "d'\(place)" : "de \(place)"118 }119120 /// Ligne sous le nom : « Saguenay–Lac-Saint-Jean · 64 produits au répertoire ».121 var countLine: String {122 let count = Fmt.products(n) + " au répertoire"123 if city != nil, let region { return region + " · " + count }124 return count125 }126127 /// Ligne d'une carte : « Boutique de Chicoutimi · 64 produits ».128 var cardLine: String { eyebrow + " · " + Fmt.products(n) }129130 /// Phrase d'origine honnête : on dit ce qu'est la boutique, on n'atteste rien du produit.131 var originSentence: String {132 // le service publie un libellé neutre (`origin_label`) : il est affiché tel quel quand il existe133 if let l = originLabel { return l.hasSuffix(".") ? l : l + "." }134 return switch origin {135 case "certifie": "La boutique déclare adhérer à un programme québécois."136 case "collectif": "Collectif d'artisans québécois."137 case "revendeur": "Détaillant québécois : il revend aussi des produits fabriqués ailleurs."138 default: "Fabricant ou artisan établi au Québec, répertorié par un annuaire."139 }140 }141142 var isReseller: Bool { origin == "revendeur" }143144 /// Photos à montrer sur une carte : trois produits de la boutique, sinon sa photo de couverture.145 var thumbnails: [String] { images.isEmpty ? [img ?? logo].compactMap { $0 } : Array(images.prefix(3)) }146}147148/// Fiche d'une boutique (`GET /api/boutiques/{id}`), ou fiche « annuaire » sans catalogue suivi.149struct StoreDetail: Decodable, Sendable {150 struct Phone: Decodable, Hashable, Sendable {151 var display: String152 var tel: String?153 enum CodingKeys: String, CodingKey { case display = "affichage", tel }154 init(from decoder: Decoder) throws {155 if let s = try? decoder.singleValueContainer().decode(String.self) { display = s; tel = s; return }156 let c = try decoder.container(keyedBy: CodingKeys.self)157 guard let d = c.text(.display) ?? c.text(.tel) else {158 throw DecodingError.dataCorruptedError(forKey: .display, in: c, debugDescription: "téléphone vide")159 }160 display = d; tel = c.text(.tel)161 }162 }163164 struct Social: Decodable, Hashable, Sendable {165 var network: String166 var url: String167 enum CodingKeys: String, CodingKey { case network, url }168 init(from decoder: Decoder) throws {169 let c = try decoder.container(keyedBy: CodingKeys.self)170 guard let u = c.text(.url) else {171 throw DecodingError.dataCorruptedError(forKey: .url, in: c, debugDescription: "réseau sans adresse")172 }173 url = u; network = c.text(.network) ?? "Réseau social"174 }175 }176177 struct Contact: Decodable, Hashable, Sendable {178 var phone: Phone?179 var email: String?180 var website: String?181 var socials: [Social] = []182 enum CodingKeys: String, CodingKey { case phone, email, website, socials }183 init() {}184 init(from decoder: Decoder) throws {185 let c = try decoder.container(keyedBy: CodingKeys.self)186 phone = c.opt(.phone); email = c.text(.email); website = c.text(.website); socials = c.list(.socials)187 }188 }189190 struct Stats: Decodable, Hashable, Sendable {191 var products: Int?192 var new: Int?193 var inStock: Int?194 var priceMin: Double?195 var priceMedian: Double?196 var priceMax: Double?197 var lastAdded: Double?198 enum CodingKeys: String, CodingKey {199 case products, new, inStock = "in_stock", priceMin = "price_min", priceMedian = "price_median"200 case priceMax = "price_max", lastAdded = "last_added"201 }202 init(from decoder: Decoder) throws {203 let c = try decoder.container(keyedBy: CodingKeys.self)204 products = c.int(.products); new = c.int(.new); inStock = c.int(.inStock)205 priceMin = c.dbl(.priceMin); priceMedian = c.dbl(.priceMedian); priceMax = c.dbl(.priceMax)206 lastAdded = c.dbl(.lastAdded)207 }208 }209210 struct Freshness: Decodable, Hashable, Sendable {211 var lastOk: Double?212 var status: String?213 var hoursSinceOk: Double?214 var cadenceHours: Double?215 enum CodingKeys: String, CodingKey {216 case lastOk = "last_ok", status, hoursSinceOk = "hours_since_ok", cadenceHours = "cadence_hours"217 }218 init(from decoder: Decoder) throws {219 let c = try decoder.container(keyedBy: CodingKeys.self)220 lastOk = c.dbl(.lastOk); status = c.text(.status); hoursSinceOk = c.dbl(.hoursSinceOk); cadenceHours = c.dbl(.cadenceHours)221 }222 }223224 /// Ce que la règle « public » écarte dans cette boutique.225 struct Exclusion: Decodable, Hashable, Sendable {226 var reason: String227 var label: String228 var n: Int229 enum CodingKeys: String, CodingKey { case reason, label, n }230 init(from decoder: Decoder) throws {231 let c = try decoder.container(keyedBy: CodingKeys.self)232 reason = c.text(.reason) ?? ""233 label = c.text(.label) ?? reason234 n = c.int(.n) ?? 0235 }236 }237238 var store: Store239 /// `false` : fiche annuaire (répertoriée, sans catalogue suivi)240 var catalogue: Bool241 var cover: String?242 var contact: Contact243 var shipping: String?244 var listedBy: [String]245 var originEvidence: String?246 var verificationDate: String?247 var regionSource: String?248 var categories: [FacetItem]249 var stats: Stats?250 var freshness: Freshness?251 var excluded: [Exclusion]252 var nearby: [Store]253 var similar: [Store]254 var products: SearchPage?255 var facets: Facets?256 var note: String?257258 enum CodingKeys: String, CodingKey {259 case catalogue, cover, contact, shipping, categories, stats, freshness, excluded, nearby, similar, products, facets, note260 case listedBy = "listed_by", originEvidence = "origin_evidence", verificationDate = "verification_date"261 case regionSource = "region_source"262 }263264 init(from decoder: Decoder) throws {265 store = try Store(from: decoder)266 let c = try decoder.container(keyedBy: CodingKeys.self)267 catalogue = c.bool(.catalogue) ?? true268 cover = c.text(.cover)269 contact = c.opt(.contact) ?? Contact()270 shipping = c.text(.shipping)271 listedBy = c.strings(.listedBy)272 originEvidence = c.text(.originEvidence)273 verificationDate = c.text(.verificationDate)274 regionSource = c.text(.regionSource)275 categories = c.list(.categories)276 stats = c.opt(.stats)277 freshness = c.opt(.freshness)278 excluded = c.list(.excluded, Exclusion.self).filter { $0.n > 0 }279 nearby = c.list(.nearby)280 similar = c.list(.similar)281 products = c.opt(.products)282 facets = c.opt(.facets)283 note = c.text(.note)284 }285286 /// Adresse du site de la boutique.287 var website: String? { contact.website ?? store.url }288289 /// « Lieu vérifié » veut dire une seule chose : l'adresse de la boutique a été recoupée.290 var placeVerified: Bool { store.verification == "verified" && store.place != nil }291}292