summaryrefslogtreecommitdiff
path: root/DomainDig/FeatureAccessService.swift
blob: 535ba7cd3a1ff3ce57febcc5384cc1f0d4fa51c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import Foundation

enum FeatureTier: String, Codable, CaseIterable, Identifiable {
    case free
    case pro
    case dataPlus

    var id: String { rawValue }

    var title: String {
        switch self {
        case .free:
            return "Free"
        case .pro:
            return "Pro"
        case .dataPlus:
            return "Data+"
        }
    }
}

enum FeatureCapability: String, CaseIterable, Identifiable {
    case singleLookup
    case basicHistory
    case limitedTracking
    case workflows
    case batchOperations
    case advancedExports
    case ownershipHistory
    case dnsHistory
    case extendedSubdomains
    case domainPricing

    var id: String { rawValue }

    var title: String {
        switch self {
        case .singleLookup:
            return "Single lookup"
        case .basicHistory:
            return "Basic history"
        case .limitedTracking:
            return "Limited tracking"
        case .workflows:
            return "Workflows"
        case .batchOperations:
            return "Batch operations"
        case .advancedExports:
            return "Advanced exports"
        case .ownershipHistory:
            return "Ownership history"
        case .dnsHistory:
            return "DNS history"
        case .extendedSubdomains:
            return "Extended subdomains"
        case .domainPricing:
            return "Domain pricing"
        }
    }
}

struct FeatureEntitlements: Equatable {
    let tier: FeatureTier
    let capabilities: Set<FeatureCapability>
    let trackedDomainLimit: Int
    let workflowLimit: Int?
    let batchSizeLimit: Int?
}

struct UpgradePromptContext: Identifiable, Equatable {
    let id = UUID()
    let title: String
    let message: String
    let capability: FeatureCapability
}

enum FeatureAccessService {
    private static let effectivelyUnlimitedTrackedDomains = 5_000

    static var currentTier: FeatureTier {
        PurchaseService.cachedTier
    }

    static var entitlements: FeatureEntitlements {
        switch currentTier {
        case .free:
            return FeatureEntitlements(
                tier: .free,
                capabilities: [.singleLookup, .basicHistory, .limitedTracking, .workflows, .batchOperations],
                trackedDomainLimit: 5,
                workflowLimit: 1,
                batchSizeLimit: 10
            )
        case .pro:
            return FeatureEntitlements(
                tier: .pro,
                capabilities: [.singleLookup, .basicHistory, .limitedTracking, .workflows, .batchOperations, .advancedExports],
                trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
                workflowLimit: nil,
                batchSizeLimit: nil
            )
        case .dataPlus:
            return FeatureEntitlements(
                tier: .dataPlus,
                capabilities: Set(FeatureCapability.allCases),
                trackedDomainLimit: effectivelyUnlimitedTrackedDomains,
                workflowLimit: nil,
                batchSizeLimit: nil
            )
        }
    }

    static func hasAccess(to capability: FeatureCapability) -> Bool {
        entitlements.capabilities.contains(capability)
    }

    static func canAddTrackedDomain(currentCount: Int) -> Bool {
        currentCount < entitlements.trackedDomainLimit
    }

    static func trackedDomainLimitMessage(currentCount: Int) -> String? {
        guard currentTier == .free else { return nil }
        if currentCount >= entitlements.trackedDomainLimit {
            return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. Available in Pro."
        }
        return "Free includes up to \(entitlements.trackedDomainLimit) tracked domains."
    }

    static func canCreateWorkflow(currentCount: Int) -> Bool {
        guard hasAccess(to: .workflows) else { return false }
        guard let limit = entitlements.workflowLimit else { return true }
        return currentCount < limit
    }

    static func canRunBatch(domainCount: Int) -> Bool {
        guard hasAccess(to: .batchOperations) else { return false }
        guard let limit = entitlements.batchSizeLimit else { return true }
        return domainCount <= limit
    }

    static func upgradeMessage(for capability: FeatureCapability) -> String {
        switch capability {
        case .workflows, .batchOperations, .advancedExports:
            return "Available in Pro"
        case .ownershipHistory, .dnsHistory, .extendedSubdomains, .domainPricing:
            return "Available in Data+"
        case .limitedTracking:
            return "Tracking is limited on Free"
        case .singleLookup, .basicHistory:
            return "Included in Free"
        }
    }

    static func workflowLimitMessage(currentCount: Int) -> String? {
        guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
        return "Free includes up to \(limit) workflow."
    }

    static func batchLimitMessage(domainCount: Int) -> String? {
        guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
        return "Free runs batches up to \(limit) domains."
    }

    static func workflowAllowanceSummary(currentCount: Int) -> String? {
        guard currentTier == .free, let limit = entitlements.workflowLimit else { return nil }
        if currentCount >= limit {
            return "Free includes up to \(limit) workflow. Available in Pro."
        }
        return "Free includes up to \(limit) workflow."
    }

    static func batchAllowanceSummary() -> String? {
        guard currentTier == .free, let limit = entitlements.batchSizeLimit else { return nil }
        return "Free runs batches up to \(limit) domains."
    }

    static func upgradePromptForTrackedDomains(currentCount: Int) -> UpgradePromptContext? {
        guard currentCount >= entitlements.trackedDomainLimit else { return nil }
        return UpgradePromptContext(
            title: "Available in Pro",
            message: "Free includes up to \(entitlements.trackedDomainLimit) tracked domains. You can keep your current watchlist, but adding more requires Pro.",
            capability: .limitedTracking
        )
    }

    static func upgradePromptForWorkflows(currentCount: Int) -> UpgradePromptContext? {
        guard let limit = entitlements.workflowLimit, currentCount >= limit else { return nil }
        return UpgradePromptContext(
            title: "Available in Pro",
            message: "Free includes up to \(limit) workflow. You can keep your current workflows, but creating more requires Pro.",
            capability: .workflows
        )
    }

    static func upgradePromptForBatch(domainCount: Int) -> UpgradePromptContext? {
        guard let limit = entitlements.batchSizeLimit, domainCount > limit else { return nil }
        return UpgradePromptContext(
            title: "Available in Pro",
            message: "Free runs batches up to \(limit) domains at a time. You can continue with smaller batches or upgrade to Pro.",
            capability: .batchOperations
        )
    }

    static func upgradePrompt(for capability: FeatureCapability) -> UpgradePromptContext {
        let title: String
        switch capability {
        case .ownershipHistory, .dnsHistory, .extendedSubdomains, .domainPricing:
            title = "Available in Data+"
        default:
            title = "Available in Pro"
        }
        return UpgradePromptContext(
            title: title,
            message: upgradeMessage(for: capability),
            capability: capability
        )
    }

    static func enabledFeatureLabels() -> [String] {
        FeatureCapability.allCases
            .filter { hasAccess(to: $0) }
            .map(\.title)
    }
}