import Foundation /// Today is a projection of the existing /tasks list, not another inbox. struct NeedsToday { var running: [ThinTask] var finished: [ThinTask] init(tasks: [ThinTask], now: Date, zone: TimeZone) { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = zone running = tasks.filter { !["done", "cancelled"].contains($0.stage) && $0.needsYou.isEmpty && $0.copyHold != true && $0.copyCanSkip != true && Self.liveStep($0) != nil }.sorted(by: Self.newestFirst) finished = tasks.filter { $0.stage == "done" && $0.doneAt.map { calendar.isDate($0, inSameDayAs: now) } == true }.sorted { let lhs = $0.doneAt ?? .distantPast, rhs = $1.doneAt ?? .distantPast if lhs != rhs { return lhs > rhs } return $0.id > $1.id } } static func completionStatus(_ task: ThinTask, now: Date, zone: TimeZone, locale: Locale) -> String? { let words = TimeWords(now: now, zone: zone, locale: locale) if let merged = task.merged { guard let at = merged.at, let into = merged.into else { return nil } // COPY APPROVED: Luna, 2026-11-26. This slot is a time, not the web's commit. return String(format: EquipoStrings.text("native.needs.merged_at"), into, words.clockOrDay(at)) } guard let at = task.doneAt else { return nil } // COPY APPROVED: Luna, 2026-11-26. return String(format: EquipoStrings.text("native.needs.done_at"), words.clockOrDay(at)) } static func liveStep(_ task: ThinTask) -> ThinStep? { guard let last = task.steps?.last, ["running", "queued"].contains(last.state) else { return nil } return last } private static func newestFirst(_ lhs: ThinTask, _ rhs: ThinTask) -> Bool { if lhs.updatedAt != rhs.updatedAt { return lhs.updatedAt > rhs.updatedAt } return lhs.id > rhs.id } }