// Models/LocalFacility.swift // -------------------------- // SwiftData model for locally cached facilities. // Populated by SyncManager.pullReferenceData() and never written by the inspector. import Foundation import SwiftData @Model final class LocalFacility { /// The server's primary key — used to match server records to local ones @Attribute(.unique) var serverId: Int var name: String var address: String var contactPerson: String var projectId: Int var projectName: String var isActive: Bool var lastSyncedAt: Date /// Areas are stored as a separate model, linked by facilityServerId @Relationship(deleteRule: .cascade) var areas: [LocalArea] init(from api: APIFacility) { self.serverId = api.id self.name = api.name self.address = api.address self.contactPerson = api.contactPerson self.projectId = api.projectId ?? 0 self.projectName = api.projectName ?? "No Contract" self.isActive = api.isActive self.lastSyncedAt = Date() self.areas = [] } func update(from api: APIFacility) { self.name = api.name self.address = api.address self.contactPerson = api.contactPerson self.projectId = api.projectId ?? 0 self.projectName = api.projectName ?? "No Contract" self.isActive = api.isActive self.lastSyncedAt = Date() } }