DomainDig/FeatureAccessService.swift
276 lines · 10002 bytes
1import Foundation
2
3enum FeatureTier: String, Codable, CaseIterable, Identifiable {
4 case free
5 case pro
6 case proPlus = "dataPlus"
7
8 var id: String { rawValue }
9
10 var title: String {
11 switch self {
12 case .free:
13 return "Free"
14 case .pro:
15 return "Pro"
16 case .proPlus:
17 return "Pro+"
18 }
19 }
20}
21
22enum FeatureCapability: String, CaseIterable, Identifiable {
23 case singleLookup
24 case basicHistory
25 case limitedTracking
26 case workflows
27 case batchOperations
28 case automatedMonitoring
29 case localAlerts
30 case advancedExports
31 case extendedSubdomains
32 case sharedPortfolios
33 case auditDepth
34 case intelligenceDetail
35 case bucketExport
36
37 var id: String { rawValue }
38
39 var title: String {
40 switch self {
41 case .singleLookup:
42 return "Single lookup"
43 case .basicHistory:
44 return "Basic history"
45 case .limitedTracking:
46 return "Limited tracking"
47 case .workflows:
48 return "Workflows"
49 case .batchOperations:
50 return "Batch operations"
51 case .automatedMonitoring:
52 return "Background monitoring"
53 case .localAlerts:
54 return "Local alerts"
55 case .advancedExports:
56 return "Advanced exports"
57 case .extendedSubdomains:
58 return "Extended subdomains"
59 case .sharedPortfolios:
60 return "Shared portfolios"
61 case .auditDepth:
62 return "Unlimited audits"
63 case .intelligenceDetail:
64 return "Intelligence detail"
65 case .bucketExport:
66 return "Export to your own bucket"
67 }
68 }
69}
70
71struct FeatureEntitlements: Equatable {
72 let tier: FeatureTier
73 let capabilities: Set<FeatureCapability>
74 let trackedDomainLimit: Int
75 let workflowLimit: Int?
76 let batchSizeLimit: Int?
77 /// Concurrently open audit sessions; nil is unlimited.
78 let activeAuditSessionLimit: Int?
79 /// Snapshots kept per install, the retention dial D1 settled on. Not
80 /// optional: every tier has a ceiling, and the store is not a place to
81 /// accumulate without one.
82 let historyRetentionLimit: Int
83}
84
85struct UpgradePromptContext: Identifiable, Equatable {
86 let id = UUID()
87 let title: String
88 let message: String
89 let capability: FeatureCapability
90}
91
92enum FeatureAccessService {
93 private static let effectivelyUnlimitedTrackedDomains = 5_000
94
95 /// Reads the observable property rather than `PurchaseService.cachedTier`,
96 /// the static over UserDefaults. SwiftUI records a dependency for any
97 /// observable property read while a body evaluates, however deep in the
98 /// call tree, so every `hasAccess` call site in a view now updates when a
99 /// purchase lands. The nine `let _ = purchaseService.currentTier` discards
100 /// that used to manufacture that dependency are gone with this.
101 static var currentTier: FeatureTier {
102 PurchaseService.shared.currentTier
103 }
104
105 static var entitlements: FeatureEntitlements {
106 entitlements(for: currentTier)
107 }
108
109 static func entitlements(for tier: FeatureTier) -> FeatureEntitlements {
110 switch tier {
111 case .free:
112 return FeatureEntitlements(
113 tier: .free,
114 capabilities: [.singleLookup, .basicHistory, .limitedTracking, .workflows, .batchOperations],
115 trackedDomainLimit: 5,
116 workflowLimit: 1,
117 batchSizeLimit: 10,
118 activeAuditSessionLimit: 1,
119 historyRetentionLimit: 50
120 )
121 case .pro:
122 return FeatureEntitlements(
123 tier: .pro,
124 capabilities: [
125 .singleLookup,
126 .basicHistory,
127 .limitedTracking,
128 .workflows,
129 .batchOperations,
130 .automatedMonitoring,
131 .localAlerts,
132 .advancedExports,
133 .extendedSubdomains
134 ],
135 trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
136 workflowLimit: nil,
137 batchSizeLimit: nil,
138 activeAuditSessionLimit: nil,
139 historyRetentionLimit: 250
140 )
141 case .proPlus:
142 return FeatureEntitlements(
143 tier: .proPlus,
144 capabilities: Set(FeatureCapability.allCases),
145 trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
146 workflowLimit: nil,
147 batchSizeLimit: nil,
148 activeAuditSessionLimit: nil,
149 historyRetentionLimit: 2_000
150 )
151 }
152 }
153
154 /// Snapshots this tier keeps. A downgrade lowers what the store may grow
155 /// to; it never prunes what is already there (Review Focus #6).
156 static var historyRetentionLimit: Int {
157 entitlements.historyRetentionLimit
158 }
159
160 static func hasAccess(to capability: FeatureCapability) -> Bool {
161 entitlements.capabilities.contains(capability)
162 }
163
164 static func canAddTrackedDomain(currentCount: Int) -> Bool {
165 canAddTrackedDomain(currentCount: currentCount, tier: currentTier)
166 }
167
168 static func canAddTrackedDomain(currentCount: Int, tier: FeatureTier) -> Bool {
169 currentCount < entitlements(for: tier).trackedDomainLimit
170 }
171
172 static func trackedDomainLimitMessage(currentCount: Int) -> String? {
173 guard currentTier == .free else { return nil }
174 if currentCount >= entitlements.trackedDomainLimit {
175 return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. Available in Pro."
176 }
177 return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains."
178 }
179
180 static func canCreateWorkflow(currentCount: Int) -> Bool {
181 guard hasAccess(to: .workflows) else { return false }
182 guard let limit = entitlements.workflowLimit else { return true }
183 return currentCount < limit
184 }
185
186 static func canRunBatch(domainCount: Int) -> Bool {
187 guard hasAccess(to: .batchOperations) else { return false }
188 guard let limit = entitlements.batchSizeLimit else { return true }
189 return domainCount <= limit
190 }
191
192 static func upgradeMessage(for capability: FeatureCapability) -> String {
193 switch capability {
194 case .workflows, .batchOperations, .automatedMonitoring, .localAlerts, .advancedExports,
195 .extendedSubdomains:
196 return "Available in Pro"
197 case .sharedPortfolios, .auditDepth, .intelligenceDetail, .bucketExport:
198 return "Available in Pro+"
199 case .limitedTracking:
200 return "Tracking is limited on Free"
201 case .singleLookup, .basicHistory:
202 return "Included in Free"
203 }
204 }
205
206 static func workflowLimitMessage(currentCount: Int) -> String? {
207 guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
208 return "Free includes up to \(limit) workflow."
209 }
210
211 static func batchLimitMessage(domainCount: Int) -> String? {
212 guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
213 return "Free runs batches up to \(limit) domains."
214 }
215
216 static func workflowAllowanceSummary(currentCount: Int) -> String? {
217 guard currentTier == .free, let limit = entitlements.workflowLimit else { return nil }
218 if currentCount >= limit {
219 return "Free includes up to \(limit) workflow. Available in Pro."
220 }
221 return "Free includes up to \(limit) workflow."
222 }
223
224 static func batchAllowanceSummary() -> String? {
225 guard currentTier == .free, let limit = entitlements.batchSizeLimit else { return nil }
226 return "Free runs batches up to \(limit) domains."
227 }
228
229 static func upgradePromptForTrackedDomains(currentCount: Int) -> UpgradePromptContext? {
230 guard currentCount >= entitlements.trackedDomainLimit else { return nil }
231 return UpgradePromptContext(
232 title: "Available in Pro",
233 message: "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. You can keep your current watchlist, but adding more requires Pro.",
234 capability: .limitedTracking
235 )
236 }
237
238 static func upgradePromptForWorkflows(currentCount: Int) -> UpgradePromptContext? {
239 guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
240 return UpgradePromptContext(
241 title: "Available in Pro",
242 message: "Free includes up to \(limit) workflow. You can keep your current workflows, but creating more requires Pro.",
243 capability: .workflows
244 )
245 }
246
247 static func upgradePromptForBatch(domainCount: Int) -> UpgradePromptContext? {
248 guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
249 return UpgradePromptContext(
250 title: "Available in Pro",
251 message: "Free runs batches up to \(limit) domains at a time. You can continue with smaller batches or upgrade to Pro.",
252 capability: .batchOperations
253 )
254 }
255
256 static func upgradePrompt(for capability: FeatureCapability) -> UpgradePromptContext {
257 let title: String
258 switch capability {
259 case .sharedPortfolios, .auditDepth, .intelligenceDetail, .bucketExport:
260 title = "Available in Pro+"
261 default:
262 title = "Available in Pro"
263 }
264 return UpgradePromptContext(
265 title: title,
266 message: upgradeMessage(for: capability),
267 capability: capability
268 )
269 }
270
271 static func enabledFeatureLabels() -> [String] {
272 FeatureCapability.allCases
273 .filter { hasAccess(to: $0) }
274 .map(\.title)
275 }
276}