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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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package subscription

import (
	"encoding/json"
	"fmt"
	"reflect"
	"slices"

	"github.com/samber/lo"
	"github.com/wI2L/jsondiff"

	"github.com/openmeterio/openmeter/openmeter/customer"
	"github.com/openmeterio/openmeter/openmeter/entitlement"
	meteredentitlement "github.com/openmeterio/openmeter/openmeter/entitlement/metered"
	"github.com/openmeterio/openmeter/openmeter/productcatalog"
	"github.com/openmeterio/openmeter/openmeter/productcatalog/feature"
	"github.com/openmeterio/openmeter/pkg/convert"
	"github.com/openmeterio/openmeter/pkg/datetime"
	"github.com/openmeterio/openmeter/pkg/models"
)

type SubscriptionView struct {
	Subscription Subscription            `json:"subscription"`
	Customer     customer.Customer       `json:"customer"`
	Spec         SubscriptionSpec        `json:"spec"`
	Phases       []SubscriptionPhaseView `json:"phases"`
}

func (s SubscriptionView) AsSpec() SubscriptionSpec {
	return s.Spec
}

func (s SubscriptionView) GetPhaseByKey(key string) (*SubscriptionPhaseView, bool) {
	for _, phase := range s.Phases {
		if phase.SubscriptionPhase.Key == key {
			return &phase, true
		}
	}
	return nil, false
}

func (s *SubscriptionView) Validate(includePhases bool) error {
	spec := s.Spec

	if s.Subscription.BillingAnchor.Compare(spec.BillingAnchor) != 0 {
		return fmt.Errorf("subscription billing anchor %v does not match spec billing anchor %v", s.Subscription.BillingAnchor, spec.BillingAnchor)
	}

	if spec.ActiveFrom.Compare(s.Subscription.ActiveFrom) != 0 {
		return fmt.Errorf("subscription active from %v does not match spec active from %v", s.Subscription.ActiveFrom, spec.ActiveFrom)
	}
	if (spec.ActiveTo == nil && s.Subscription.ActiveTo != nil) ||
		(spec.ActiveTo != nil && s.Subscription.ActiveTo == nil) || (spec.ActiveTo != nil && s.Subscription.ActiveTo != nil && spec.ActiveTo.Compare(*s.Subscription.ActiveTo) != 0) {
		return fmt.Errorf("subscription active to %v does not match spec active to %v", s.Subscription.ActiveTo, spec.ActiveTo)
	}

	if spec.CustomerId != s.Subscription.CustomerId {
		return fmt.Errorf("subscription customer id %s does not match spec customer id %s", s.Subscription.CustomerId, spec.CustomerId)
	}

	if spec.Currency != s.Subscription.Currency {
		return fmt.Errorf("subscription currency %s does not match spec currency %s", s.Subscription.Currency, spec.Currency)
	}

	if !spec.Plan.NilEqual(s.Subscription.PlanRef) {
		return fmt.Errorf("subscription plan %v does not match spec plan %v", s.Subscription.PlanRef, spec.Plan)
	}

	if includePhases {
		for _, phase := range s.Phases {
			if err := phase.Validate(true); err != nil {
				return fmt.Errorf("phase %s is invalid: %w", phase.Spec.PhaseKey, err)
			}
		}
	}

	if spec.SettlementMode != s.Subscription.SettlementMode {
		return fmt.Errorf("subscription settlement mode %s does not match spec settlement mode %s", s.Subscription.SettlementMode, spec.SettlementMode)
	}

	return nil
}

type SubscriptionPhaseView struct {
	SubscriptionPhase SubscriptionPhase                 `json:"subscriptionPhase"`
	Spec              SubscriptionPhaseSpec             `json:"spec"`
	ItemsByKey        map[string][]SubscriptionItemView `json:"itemsByKey"`
}

func (s *SubscriptionPhaseView) AsSpec() SubscriptionPhaseSpec {
	return s.Spec
}

func (s *SubscriptionPhaseView) Validate(includeItems bool) error {
	if includeItems {
		for _, items := range s.ItemsByKey {
			for _, item := range items {
				if err := item.Validate(); err != nil {
					return fmt.Errorf("item %s in phase %s starting after %s is invalid: %w", item.Spec.ItemKey, item.Spec.ActiveFromOverrideRelativeToPhaseStart.ISOStringPtrOrNil(), s.Spec.PhaseKey, err)
				}
			}
		}
	}
	return nil
}

type SubscriptionItemView struct {
	SubscriptionItem SubscriptionItem     `json:"subscriptionItem"`
	Spec             SubscriptionItemSpec `json:"spec"`

	Entitlement *SubscriptionEntitlement `json:"entitlement,omitempty"`
	Feature     *feature.Feature         `json:"feature,omitempty"`
}

func (s *SubscriptionItemView) AsSpec() SubscriptionItemSpec {
	return s.Spec
}

func (s *SubscriptionItemView) Validate() error {
	// Let's validate that the RateCard contents match in Spec and SubscriptionItem
	if !s.Spec.RateCard.Equal(s.SubscriptionItem.RateCard) {
		return fmt.Errorf("item %s rate card %+v does not match spec rate card %+v", s.Spec.ItemKey, s.SubscriptionItem.RateCard, s.Spec.RateCard)
	}

	// Let's validate whether it should have an entitlement
	if (s.Entitlement == nil) != (s.SubscriptionItem.RateCard.AsMeta().EntitlementTemplate == nil) {
		return fmt.Errorf("item %s should have an entitlement: %v", s.Spec.ItemKey, s.SubscriptionItem.RateCard.AsMeta().EntitlementTemplate)
	}

	// Let's validate the Entitlement looks as it should
	if s.Entitlement != nil && s.SubscriptionItem.RateCard.AsMeta().EntitlementTemplate != nil {
		// First, lets validate the nested model
		if err := s.Entitlement.Validate(); err != nil {
			return fmt.Errorf("entitlement for item %s is invalid: %w", s.Spec.ItemKey, err)
		}

		// Second, let's validate the linking
		if !reflect.DeepEqual(&s.Entitlement.Entitlement.ID, s.SubscriptionItem.EntitlementID) {
			return fmt.Errorf("entitlement %s does not match item %s entitlement id", s.Entitlement.Entitlement.ID, s.Spec.ItemKey)
		}

		// Third, let's validate it looks according to the Template
		tpl := s.SubscriptionItem.RateCard.AsMeta().EntitlementTemplate
		ent := s.Entitlement.Entitlement

		switch tpl.Type() {
		case entitlement.EntitlementTypeBoolean:
			if ent.EntitlementType != entitlement.EntitlementTypeBoolean {
				return fmt.Errorf("entitlement %s is not boolean", s.Entitlement.Entitlement.ID)
			}
		case entitlement.EntitlementTypeStatic:
			if ent.EntitlementType != entitlement.EntitlementTypeStatic {
				return fmt.Errorf("entitlement %s is not static", s.Entitlement.Entitlement.ID)
			}

			e, err := tpl.AsStatic()
			if err != nil {
				return fmt.Errorf("entitlement template for Item %s is not static: %w", s.SubscriptionItem.Key, err)
			}

			var configJSON string

			err = json.Unmarshal(e.Config, &configJSON)
			if err != nil {
				return fmt.Errorf("entitlement template for Item %s has invalid JSON config: %w", s.SubscriptionItem.Key, err)
			}

			diff, err := jsondiff.CompareJSON([]byte(configJSON), []byte(lo.FromPtr(ent.Config)))
			if err != nil {
				return fmt.Errorf("failed to compare entitlement with template config for Item %s: %w", s.SubscriptionItem.Key, err)
			}

			if len(diff) > 0 {
				return fmt.Errorf("entitlement %s config does not match template config", s.Entitlement.Entitlement.ID)
			}

		case entitlement.EntitlementTypeMetered:
			mEnt, err := meteredentitlement.ParseFromGenericEntitlement(&ent.Entitlement)
			if err != nil {
				return fmt.Errorf("entitlement %s is not metered: %w", s.Entitlement.Entitlement.ID, err)
			}

			e, err := tpl.AsMetered()
			if err != nil {
				return fmt.Errorf("entitlement template for Item %s is not metered: %w", s.SubscriptionItem.Key, err)
			}

			if e.IsSoftLimit != mEnt.IsSoftLimit {
				return fmt.Errorf("entitlement %s isSoftLimit does not match template isSoftLimit", s.Entitlement.Entitlement.ID)
			}

			if !reflect.DeepEqual(e.IssueAfterReset, convert.SafeDeRef(mEnt.IssueAfterReset, func(m meteredentitlement.IssueAfterReset) *float64 {
				return &m.Amount
			})) {
				return fmt.Errorf("entitlement %s issueAfterReset does not match template issueAfterReset", s.Entitlement.Entitlement.ID)
			}

			if !reflect.DeepEqual(e.IssueAfterResetPriority, convert.SafeDeRef(mEnt.IssueAfterReset, func(m meteredentitlement.IssueAfterReset) *uint8 {
				return m.Priority
			})) {
				return fmt.Errorf("entitlement %s issueAfterResetPriority does not match template issueAfterResetPriority", s.Entitlement.Entitlement.ID)
			}

			// FIXME: instead of this defaulting behavior we should align the types so that MeteredEntitlementTemplate has the same required fields as MeteredEntitlement
			if !reflect.DeepEqual(lo.CoalesceOrEmpty(e.PreserveOverageAtReset, lo.ToPtr(false)), &mEnt.PreserveOverageAtReset) {
				return fmt.Errorf("entitlement %s preserveOverageAtReset does not match template preserveOverageAtReset", s.Entitlement.Entitlement.ID)
			}

		default:
			return fmt.Errorf("entitlement type %s is not supported", s.SubscriptionItem.RateCard.AsMeta().EntitlementTemplate.Type())
		}
	}

	// Let's validate the Feature
	if s.Feature != nil {
		if s.SubscriptionItem.RateCard.AsMeta().FeatureKey == nil {
			return fmt.Errorf("item %s has a feature, but no feature key", s.Spec.ItemKey)
		}

		// If it has an entitlement lets compare to the ID, otherwise let's compare the key
		if s.Entitlement != nil {
			if s.Entitlement.Entitlement.FeatureID != s.Feature.ID {
				return fmt.Errorf("entitlement %s feature id %s does not match item %s feature id %s", s.Entitlement.Entitlement.ID, s.Entitlement.Entitlement.FeatureID, s.Spec.ItemKey, s.Feature.ID)
			}
		} else {
			if *s.SubscriptionItem.RateCard.AsMeta().FeatureKey != s.Feature.Key {
				return fmt.Errorf("item %s feature key %s does not match feature key %s", s.Spec.ItemKey, *s.SubscriptionItem.RateCard.AsMeta().FeatureKey, s.Feature.Key)
			}
		}
	}

	return nil
}

func NewSubscriptionView(
	sub Subscription,
	cust customer.Customer,
	phases []SubscriptionPhase,
	items []SubscriptionItem,
	ents []SubscriptionEntitlement,
	entFeats []feature.Feature,
	itemFeats []feature.Feature,
) (*SubscriptionView, error) {
	spec := SubscriptionSpec{
		CreateSubscriptionPlanInput: CreateSubscriptionPlanInput{
			Plan:            sub.PlanRef,
			BillingCadence:  sub.BillingCadence,
			ProRatingConfig: sub.ProRatingConfig,
			SettlementMode:  sub.SettlementMode,
		},
		CreateSubscriptionCustomerInput: CreateSubscriptionCustomerInput{
			CustomerId:    sub.CustomerId,
			Currency:      sub.Currency,
			ActiveFrom:    sub.ActiveFrom,
			ActiveTo:      sub.ActiveTo,
			MetadataModel: sub.MetadataModel,
			Name:          sub.Name,
			Description:   sub.Description,
			BillingAnchor: sub.BillingAnchor,
		},
		Phases: make(map[string]*SubscriptionPhaseSpec),
	}

	view := &SubscriptionView{
		Subscription: sub,
		Customer:     cust,
	}

	// Let's validate that all items are used
	unvisitedItems := make(map[string]struct{})
	for _, item := range items {
		// And also that there are no duplicates
		if _, ok := unvisitedItems[item.ID]; ok {
			return nil, fmt.Errorf("item %s is duplicated", item.ID)
		}

		unvisitedItems[item.ID] = struct{}{}
	}

	// Lets validate that all ents are used
	unvisitedEnts := map[string]struct{}{}
	for _, ent := range ents {
		// While here, lets also validate that there are no duplicates
		if _, ok := unvisitedEnts[ent.Entitlement.ID]; ok {
			return nil, fmt.Errorf("entitlement %s is duplicated", ent.Entitlement.ID)
		}

		unvisitedEnts[ent.Entitlement.ID] = struct{}{}
	}

	// Let's sort the phases
	sortedPhases := make([]SubscriptionPhase, len(phases))
	copy(sortedPhases, phases)
	slices.SortStableFunc(sortedPhases, func(i, j SubscriptionPhase) int {
		return i.ActiveFrom.Compare(j.ActiveFrom)
	})

	itemsByPhase := lo.GroupBy(items, func(item SubscriptionItem) string {
		return item.PhaseId
	})

	// Let's start with all the phases
	for _, phase := range sortedPhases {
		// Let's guard against duplicates
		if _, ok := spec.Phases[phase.Key]; ok {
			return nil, fmt.Errorf("phase %s is duplicated", phase.Key)
		}

		phaseStartAfter := datetime.ISODurationBetween(sub.ActiveFrom, phase.ActiveFrom)

		phaseSpec := SubscriptionPhaseSpec{
			CreateSubscriptionPhasePlanInput: CreateSubscriptionPhasePlanInput{
				PhaseKey:    phase.Key,
				StartAfter:  phaseStartAfter,
				Name:        phase.Name,
				Description: phase.Description,
				SortHint:    phase.SortHint,
			},
			CreateSubscriptionPhaseCustomerInput: CreateSubscriptionPhaseCustomerInput{
				MetadataModel: phase.MetadataModel,
			},
			ItemsByKey: make(map[string][]*SubscriptionItemSpec),
		}

		phaseView := SubscriptionPhaseView{
			SubscriptionPhase: phase,
			ItemsByKey:        make(map[string][]SubscriptionItemView),
		}

		phaseItems, ok := itemsByPhase[phase.ID]
		if !ok {
			return nil, fmt.Errorf("items for phase %s not found", phase.Key)
		}

		// Let's group the items by key
		phaseItemsByKey := lo.GroupBy(phaseItems, func(item SubscriptionItem) string {
			return item.Key
		})

		// Let's sort the items by start time
		for key := range phaseItemsByKey {
			// Any arbitrary time works as long as its consistent for the comparisons
			slices.SortStableFunc(phaseItemsByKey[key], func(i, j SubscriptionItem) int {
				iT, jT := phase.ActiveFrom, phase.ActiveFrom
				if i.ActiveFromOverrideRelativeToPhaseStart != nil {
					iT, _ = i.ActiveFromOverrideRelativeToPhaseStart.AddTo(phase.ActiveFrom)
				}
				if j.ActiveFromOverrideRelativeToPhaseStart != nil {
					jT, _ = j.ActiveFromOverrideRelativeToPhaseStart.AddTo(phase.ActiveFrom)
				}
				return int(iT.Sub(jT))
			})
		}

		for key, items := range phaseItemsByKey {
			for _, item := range items {
				// Sanity check
				if item.PhaseId != phase.ID {
					return nil, fmt.Errorf("item %s of phase %s is not in the correct phase", item.Key, phase.Key)
				}

				// Sanity check 2
				if item.Key != key {
					return nil, fmt.Errorf("item %s of phase %s is not in the correct group", item.Key, phase.Key)
				}

				delete(unvisitedItems, item.ID)

				itemSpec := SubscriptionItemSpec{
					CreateSubscriptionItemInput: CreateSubscriptionItemInput{
						CreateSubscriptionItemPlanInput: CreateSubscriptionItemPlanInput{
							PhaseKey: phase.Key,
							ItemKey:  item.Key,
							RateCard: item.RateCard,
						},
						CreateSubscriptionItemCustomerInput: CreateSubscriptionItemCustomerInput{
							ActiveFromOverrideRelativeToPhaseStart: item.ActiveFromOverrideRelativeToPhaseStart,
							ActiveToOverrideRelativeToPhaseStart:   item.ActiveToOverrideRelativeToPhaseStart,
							BillingBehaviorOverride:                item.BillingBehaviorOverride,
						},
						Annotations: item.Annotations,
					},
				}

				// Let's find the entitlement

				var subEnt *SubscriptionEntitlement
				if ent, ok := lo.Find(ents, func(i SubscriptionEntitlement) bool {
					return reflect.DeepEqual(&i.Entitlement.ID, item.EntitlementID)
				}); ok {
					subEnt = &ent
					delete(unvisitedEnts, ent.Entitlement.ID)
				}

				var itemFeat *feature.Feature
				// If entitlement is present, we use the entitlement's feature, otherwise we use the item's feature
				if subEnt != nil {
					if feat, ok := lo.Find(entFeats, func(i feature.Feature) bool {
						return i.ID == subEnt.Entitlement.FeatureID
					}); ok {
						itemFeat = &feat
					}
				} else if item.RateCard.AsMeta().FeatureKey != nil {
					if feat, ok := lo.Find(itemFeats, func(i feature.Feature) bool {
						return i.Key == *item.RateCard.AsMeta().FeatureKey
					}); ok {
						itemFeat = &feat
					}
				}

				if itemFeat != nil {
					_ = item.RateCard.ChangeMeta(func(m productcatalog.RateCardMeta) (productcatalog.RateCardMeta, error) {
						m.FeatureID = lo.ToPtr(itemFeat.ID)

						return m, nil
					})
				}

				itemView := SubscriptionItemView{
					SubscriptionItem: item,
					Entitlement:      subEnt,
					Feature:          itemFeat,
					Spec:             itemSpec,
				}

				phaseSpec.ItemsByKey[key] = append(phaseSpec.ItemsByKey[item.Key], &itemSpec)
				phaseView.ItemsByKey[key] = append(phaseView.ItemsByKey[key], itemView)
			}
		}

		spec.Phases[phase.Key] = &phaseSpec
		// Let's add spec to view

		phaseView.Spec = phaseSpec

		view.Phases = append(view.Phases, phaseView)
	}

	if len(unvisitedEnts) > 0 {
		return nil, fmt.Errorf("unvisited entitlements: %v", unvisitedEnts)
	}

	if len(unvisitedItems) > 0 {
		return nil, fmt.Errorf("unvisited items: %v", unvisitedItems)
	}

	if err := spec.Validate(); err != nil {
		return nil, models.ErrorWithComponent("subscriptionspec", err)
	}

	// Let's add spec to view
	view.Spec = spec

	if err := view.Validate(true); err != nil {
		return nil, fmt.Errorf("subscription view is invalid: %w", err)
	}

	return view, nil
}