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