import Foundation /// Quando Paolo usa Claude, ora per ora della settimana (lunedì 0:00 = casella 0). /// Serve a distribuire il limite settimanale sulle ore di lavoro invece che sulle 24 ore: /// di notte la tabella di marcia sta ferma, di giorno avanza. struct Profile: Codable { /// 168 caselle: consumo (punti percentuali) attribuito a ogni ora della settimana. var bins: [Double] var updated: Date // Partenza ricavata dai log di Claude Code di novembre 2026 (4 settimane): // lavoro dalle 8 alle 21, lunedì il giorno più pieno, domenica il più leggero. private static let seedHours: [Double] = [0.15, 0, 0, 0, 0, 0, 0, 0.31, 1.78, 1.71, 2.14, 1.55, 2.14, 1.71, 1.61, 2.19, 1.91, 1.48, 1.81, 1.66, 1.15, 0.33, 0.1, 0.28] private static let seedDays: [Double] = [1.48, 1.27, 1.11, 1.08, 0.91, 0.67, 0.48] /// Come se una settimana intera (100%) fosse andata così. static func seed(now: Date = Date()) -> Profile { var bins = [Double](repeating: 0, count: 168) for d in 0..<7 { for h in 0..<24 { bins[d * 24 + h] = seedDays[d] * seedHours[h] } } let total = bins.reduce(0, +) return Profile(bins: bins.map { $0 / total * 100 }, updated: now) } /// La casella dell'ora che contiene `date`. static func slot(_ date: Date, calendar: Calendar = .current) -> Int { let c = calendar.dateComponents([.weekday, .hour], from: date) let day = ((c.weekday ?? 2) + 5) % 7 // lunedì = 0 return day * 24 + (c.hour ?? 0) } /// Aggiunge un consumo. Il passato pesa sempre meno (si dimezza ogni due settimane). mutating func add(_ amount: Double, at date: Date) { guard amount > 0 else { return } let days = max(0, date.timeIntervalSince(updated) / 86_400) let decay = pow(0.5, days / 14) bins = bins.map { $0 * decay } bins[Self.slot(date)] += amount updated = date } /// Peso di ogni ora (media 1). Giorno e ora si stimano separati, così bastano poche settimane: /// peso = quanto pesa quel giorno × forma della giornata (lisciata sulle ore vicine). func weights() -> [Double] { var hours = [Double](repeating: 0, count: 24) var days = [Double](repeating: 0, count: 7) for d in 0..<7 { for h in 0..<24 { hours[h] += bins[d * 24 + h] days[d] += bins[d * 24 + h] } } let smooth = (0..<24).map { h in 0.25 * hours[(h + 23) % 24] + 0.5 * hours[h] + 0.25 * hours[(h + 1) % 24] } var w = [Double](repeating: 0, count: 168) for d in 0..<7 { for h in 0..<24 { w[d * 24 + h] = days[d] * smooth[h] } } let mean = w.reduce(0, +) / 168 guard mean > 0 else { return [Double](repeating: 1, count: 168) } // Un minimo anche di notte, così niente resta a zero. return w.map { $0 / mean + 0.02 } } } /// L'orologio del lavoro: misura il tempo in "ore di lavoro" pesate col profilo. struct WorkClock { let weights: [Double] init(_ profile: Profile) { weights = profile.weights() } /// Quanto lavoro c'è tra due istanti (un'ora piena di peso 1 vale 1). func amount(from a: Date, to b: Date) -> Double { guard b > a else { return 0 } var total = 0.0 var t = a var slot = Profile.slot(a) var next = Calendar.current.dateInterval(of: .hour, for: a)?.end ?? a.addingTimeInterval(3600) while t < b { let end = min(next, b) total += weights[slot] * end.timeIntervalSince(t) / 3600 t = end next = next.addingTimeInterval(3600) slot = (slot + 1) % 168 } return total } /// Quando si raggiunge `work` ore di lavoro partendo da `a`; nil se dopo `limit`. func advance(from a: Date, work: Double, limit: Date) -> Date? { var left = work var t = a var slot = Profile.slot(a) var next = Calendar.current.dateInterval(of: .hour, for: a)?.end ?? a.addingTimeInterval(3600) while t < limit { let end = min(next, limit) let piece = weights[slot] * end.timeIntervalSince(t) / 3600 if piece >= left { return t.addingTimeInterval(left / weights[slot] * 3600) } left -= piece t = end next = next.addingTimeInterval(3600) slot = (slot + 1) % 168 } return nil } }