import Foundation public enum SessionState: String, Codable, Sendable { case open // accepting captures case archived // closed and kept as a past session (sent or not); pruned only by an age policy the user set } /// The manifest persisted as session.json alongside the PNGs. public struct CaptureSession: Codable, Sendable, Identifiable, Equatable { public let id: UUID public let createdAt: Date public private(set) var state: SessionState public private(set) var captures: [Capture] /// Set when the PDF is built, so a re-send reuses the same file. nil on a /// session that was closed without ever sending — the Past sessions list /// shows that as "Not sent". public private(set) var pdfFileName: String? /// Lane / deployed build / commit / reviewer / note for this session. Absent from /// manifests written before v0.5.0 — those decode as `.empty`, never a guess. public private(set) var context: ReviewContext public init( id: UUID, createdAt: Date, state: SessionState, captures: [Capture], pdfFileName: String?, context: ReviewContext = .empty ) { self.id = id self.createdAt = createdAt self.state = state self.captures = captures self.pdfFileName = pdfFileName self.context = context } public var isEmpty: Bool { captures.isEmpty } /// First 8 characters of the UUID, uppercase — the stable short handle used in /// file names, the session list and the PDF header. public var shortID: String { String(id.uuidString.replacingOccurrences(of: "-", with: "").prefix(8)).uppercased() } /// max(sequence)+1, or 1 when empty. public var nextSequence: Int { (captures.map(\.sequence).max() ?? 0) + 1 } /// Value-semantic: returns a new session; does not mutate `self`. public func appending(_ capture: Capture) -> CaptureSession { CaptureSession( id: id, createdAt: createdAt, state: state, captures: captures + [capture], pdfFileName: pdfFileName, context: context ) } /// Value-semantic: returns a new session; does not mutate `self`. /// Sequence numbers of remaining captures are left unchanged. public func removing(captureID: UUID) -> CaptureSession { CaptureSession( id: id, createdAt: createdAt, state: state, captures: captures.filter { $0.id != captureID }, pdfFileName: pdfFileName, context: context ) } /// Value-semantic: returns a new session; does not mutate `self`. /// `pdfFileName` is nil when the session is closed without ever sending. public func markArchived(pdfFileName: String?) -> CaptureSession { CaptureSession( id: id, createdAt: createdAt, state: .archived, captures: captures, pdfFileName: pdfFileName, context: context ) } /// Value-semantic: returns a new session; does not mutate `self`. public func withContext(_ newContext: ReviewContext) -> CaptureSession { CaptureSession( id: id, createdAt: createdAt, state: state, captures: captures, pdfFileName: pdfFileName, context: newContext ) } /// Value-semantic: returns a new session; does not mutate `self`. public func withPDFFileName(_ name: String?) -> CaptureSession { CaptureSession( id: id, createdAt: createdAt, state: state, captures: captures, pdfFileName: name, context: context ) } private enum CodingKeys: String, CodingKey { case id, createdAt, state, captures, pdfFileName, context } /// `context` is decoded leniently so every manifest written before v0.5.0 keeps /// loading; all other keys are required, exactly as before. public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.init( id: try container.decode(UUID.self, forKey: .id), createdAt: try container.decode(Date.self, forKey: .createdAt), state: try container.decode(SessionState.self, forKey: .state), captures: try container.decode([Capture].self, forKey: .captures), pdfFileName: try container.decodeIfPresent(String.self, forKey: .pdfFileName), context: try container.decodeIfPresent(ReviewContext.self, forKey: .context) ?? .empty ) } }