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
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
package httpdriver

import (
	"context"
	"fmt"
	"net/http"

	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/api"
	"github.com/openmeterio/openmeter/openmeter/app"
	appcustominvoicing "github.com/openmeterio/openmeter/openmeter/app/custominvoicing"
	appsandbox "github.com/openmeterio/openmeter/openmeter/app/sandbox"
	appstripe "github.com/openmeterio/openmeter/openmeter/app/stripe"
	"github.com/openmeterio/openmeter/openmeter/billing"
	"github.com/openmeterio/openmeter/openmeter/customer"
	"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
	"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
	"github.com/openmeterio/openmeter/pkg/models"
	"github.com/openmeterio/openmeter/pkg/pagination"
)

type (
	ListCustomerDataRequest  = app.ListCustomerInput
	ListCustomerDataResponse = api.CustomerAppDataPaginatedResponse
	ListCustomerDataHandler  httptransport.HandlerWithArgs[ListCustomerDataRequest, ListCustomerDataResponse, ListCustomerDataParams]
)

type ListCustomerDataParams struct {
	api.ListCustomerAppDataParams
	CustomerIdOrKey string
}

// ListCustomerData returns a handler for listing customers app data.
func (h *handler) ListCustomerData() ListCustomerDataHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, params ListCustomerDataParams) (ListCustomerDataRequest, error) {
			ns, err := h.resolveNamespace(ctx)
			if err != nil {
				return ListCustomerDataRequest{}, err
			}

			// Get the customer
			cus, err := h.customerService.GetCustomer(ctx, customer.GetCustomerInput{
				CustomerIDOrKey: &customer.CustomerIDOrKey{
					IDOrKey:   params.CustomerIdOrKey,
					Namespace: ns,
				},
			})
			if err != nil {
				return ListCustomerDataRequest{}, err
			}

			if cus != nil && cus.IsDeleted() {
				return ListCustomerDataRequest{},
					models.NewGenericPreConditionFailedError(
						fmt.Errorf("customer is deleted [namespace=%s customer.id=%s]", cus.Namespace, cus.ID),
					)
			}

			req := ListCustomerDataRequest{
				CustomerID: cus.GetID(),

				// Pagination
				Page: pagination.Page{
					PageSize:   lo.FromPtrOr(params.PageSize, customer.DefaultPageSize),
					PageNumber: lo.FromPtrOr(params.Page, customer.DefaultPageNumber),
				},
			}

			if params.Type != nil {
				req.Type = lo.ToPtr(app.AppType(*params.Type))
			}

			return req, nil
		},
		func(ctx context.Context, request ListCustomerDataRequest) (ListCustomerDataResponse, error) {
			resp, err := h.service.ListCustomerData(ctx, request)
			if err != nil {
				return ListCustomerDataResponse{}, fmt.Errorf("failed to list customers: %w", err)
			}

			items := make([]api.CustomerAppData, 0, len(resp.Items))

			for _, customerApp := range resp.Items {
				item, err := h.toAPICustomerAppData(customerApp)
				if err != nil {
					return ListCustomerDataResponse{}, fmt.Errorf("failed to cast app customer data: %w", err)
				}

				items = append(items, item)
			}

			return ListCustomerDataResponse{
				Items:      items,
				Page:       resp.Page.PageNumber,
				PageSize:   resp.Page.PageSize,
				TotalCount: resp.TotalCount,
			}, nil
		},
		commonhttp.JSONResponseEncoderWithStatus[ListCustomerDataResponse](http.StatusOK),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("listCustomerData"),
		)...,
	)
}

type UpsertCustomerDataRequest struct {
	CustomerId customer.CustomerID
	Data       []api.CustomerAppData
}

type UpsertCustomerDataParams struct {
	CustomerIdOrKey string
}

type (
	UpsertCustomerDataResponse = interface{}
	UpsertCustomerDataHandler  httptransport.HandlerWithArgs[UpsertCustomerDataRequest, UpsertCustomerDataResponse, UpsertCustomerDataParams]
)

// UpsertCustomerData returns a new httptransport.Handler for creating a customer.
func (h *handler) UpsertCustomerData() UpsertCustomerDataHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, params UpsertCustomerDataParams) (UpsertCustomerDataRequest, error) {
			ns, err := h.resolveNamespace(ctx)
			if err != nil {
				return UpsertCustomerDataRequest{}, err
			}

			// Get the customer and ensure we do not update customer data if the customer is already deleted
			cus, err := h.customerService.GetCustomer(ctx, customer.GetCustomerInput{
				CustomerIDOrKey: &customer.CustomerIDOrKey{
					IDOrKey:   params.CustomerIdOrKey,
					Namespace: ns,
				},
			})
			if err != nil {
				return UpsertCustomerDataRequest{}, err
			}

			if cus != nil && cus.IsDeleted() {
				return UpsertCustomerDataRequest{},
					models.NewGenericPreConditionFailedError(
						fmt.Errorf("customer is deleted [namespace=%s customer.id=%s]", cus.Namespace, cus.ID),
					)
			}

			var body []api.CustomerAppData

			if err := commonhttp.JSONRequestBodyDecoder(r, &body); err != nil {
				return UpsertCustomerDataRequest{},
					fmt.Errorf("field to decode upsert customer data request: %w", err)
			}

			return UpsertCustomerDataRequest{
				CustomerId: cus.GetID(),
				Data:       body,
			}, nil
		},
		func(ctx context.Context, req UpsertCustomerDataRequest) (UpsertCustomerDataResponse, error) {
			for _, apiCustomerData := range req.Data {
				customerApp, customerData, err := h.toCustomerData(ctx, req.CustomerId, apiCustomerData)
				if err != nil {
					return nil, err
				}

				err = customerApp.UpsertCustomerData(ctx, app.UpsertAppInstanceCustomerDataInput{
					CustomerID: req.CustomerId,
					Data:       customerData,
				})
				if err != nil {
					return nil, err
				}
			}

			return nil, nil
		},
		commonhttp.EmptyResponseEncoder[UpsertCustomerDataResponse](http.StatusOK),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("upsertCustomerData"),
		)...,
	)
}

type DeleteCustomerDataParams struct {
	CustomerIdOrKey string
	AppId           string
}

type DeleteCustomerDataRequest struct {
	AppID      app.AppID
	CustomerID customer.CustomerID
}

type (
	DeleteCustomerDataResponse = interface{}
	DeleteCustomerDataHandler  httptransport.HandlerWithArgs[DeleteCustomerDataRequest, DeleteCustomerDataResponse, DeleteCustomerDataParams]
)

// DeleteCustomerData returns a handler for deleting a customer data.
func (h *handler) DeleteCustomerData() DeleteCustomerDataHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, params DeleteCustomerDataParams) (DeleteCustomerDataRequest, error) {
			ns, err := h.resolveNamespace(ctx)
			if err != nil {
				return DeleteCustomerDataRequest{}, err
			}

			// Get the customer
			cus, err := h.customerService.GetCustomer(ctx, customer.GetCustomerInput{
				CustomerIDOrKey: &customer.CustomerIDOrKey{
					IDOrKey:   params.CustomerIdOrKey,
					Namespace: ns,
				},
			})
			if err != nil {
				return DeleteCustomerDataRequest{}, err
			}

			if cus != nil && cus.IsDeleted() {
				return DeleteCustomerDataRequest{},
					models.NewGenericPreConditionFailedError(
						fmt.Errorf("customer is deleted [namespace=%s customer.id=%s]", cus.Namespace, cus.ID),
					)
			}

			return DeleteCustomerDataRequest{
				CustomerID: cus.GetID(),
				AppID: app.AppID{
					Namespace: ns,
					ID:        params.AppId,
				},
			}, nil
		},
		func(ctx context.Context, request DeleteCustomerDataRequest) (DeleteCustomerDataResponse, error) {
			// Get app
			existingApp, err := h.service.GetApp(ctx, request.AppID)
			if err != nil {
				return nil, err
			}

			// Delete customer data
			err = existingApp.DeleteCustomerData(ctx, app.DeleteAppInstanceCustomerDataInput{
				CustomerID: request.CustomerID,
			})
			if err != nil {
				return nil, err
			}

			return nil, nil
		},
		commonhttp.EmptyResponseEncoder[DeleteCustomerDataResponse](http.StatusNoContent),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("deleteCustomerData"),
		)...,
	)
}

// toCustomerData converts an API CustomerAppData to a CustomerData model
func (h *handler) toCustomerData(ctx context.Context, customerID customer.CustomerID, apiApp api.CustomerAppData) (app.App, app.CustomerData, error) {
	// Get app type
	appType, err := apiApp.Discriminator()
	if err != nil {
		return nil, nil, fmt.Errorf("error getting app type: %w", err)
	}

	switch appType {
	// Sandbox app
	case string(app.AppTypeSandbox):
		// Parse as sandbox app
		apiSandboxCustomerData, err := apiApp.AsSandboxCustomerAppData()
		if err != nil {
			return nil, nil, fmt.Errorf("error converting to stripe app: %w", err)
		}

		// Resolve app
		resolvedApp, err := h.resolveCustomerApp(ctx, customerID, app.AppTypeSandbox, apiSandboxCustomerData.Id)
		if err != nil {
			return nil, nil, fmt.Errorf("error resolving sandbox app: %w", err)
		}

		// Create customer data
		sandboxCustomerData := appsandbox.CustomerData{}

		return resolvedApp, sandboxCustomerData, nil

	// Stripe app
	case string(app.AppTypeStripe):
		// Parse as stripe app
		apiStripeCustomerData, err := apiApp.AsStripeCustomerAppData()
		if err != nil {
			return nil, nil, fmt.Errorf("error converting to stripe app: %w", err)
		}

		// Resolve app
		resolvedApp, err := h.resolveCustomerApp(ctx, customerID, app.AppTypeStripe, apiStripeCustomerData.Id)
		if err != nil {
			return nil, nil, fmt.Errorf("error resolving stripe app: %w", err)
		}

		// Create customer data
		stripeCustomerData := fromAPIAppStripeCustomerData(apiStripeCustomerData)

		return resolvedApp, stripeCustomerData, nil
	case string(app.AppTypeCustomInvoicing):
		// Parse as custom invoicing app
		apiCustomInvoicingCustomerData, err := apiApp.AsCustomInvoicingCustomerAppData()
		if err != nil {
			return nil, nil, fmt.Errorf("error converting to custom invoicing app: %w", err)
		}

		// Resolve app
		resolvedApp, err := h.resolveCustomerApp(ctx, customerID, app.AppTypeCustomInvoicing, apiCustomInvoicingCustomerData.Id)
		if err != nil {
			return nil, nil, fmt.Errorf("error resolving custom invoicing app: %w", err)
		}

		// Create customer data
		customInvoicingCustomerData := appcustominvoicing.CustomerData{
			Metadata: lo.FromPtrOr(apiCustomInvoicingCustomerData.Metadata, map[string]string{}),
		}

		return resolvedApp, customInvoicingCustomerData, nil
	}

	return nil, nil, fmt.Errorf("unsupported app type: %s", appType)
}

// resolveCustomerApp resolves a customer app based on the app type or app ID.
func (h *handler) resolveCustomerApp(ctx context.Context, customerID customer.CustomerID, appType app.AppType, appID *string) (app.App, error) {
	var resolvedApp app.App
	var err error

	// Get app ID from API data or get default app for billing profile
	if appID != nil {
		return h.service.GetApp(ctx, app.GetAppInput{
			Namespace: customerID.Namespace,
			ID:        *appID,
		})
	}

	// Get the customer app by type
	resolvedApp, err = h.billingService.GetCustomerApp(ctx, billing.GetCustomerAppInput{
		CustomerID: customerID,
		AppType:    appType,
	})
	if err != nil {
		return nil, fmt.Errorf("error getting customer app: %w", err)
	}

	return resolvedApp, nil
}

// toAPICustomerAppData converts a CustomerApp to an API CustomerAppData
func (h *handler) toAPICustomerAppData(a app.CustomerApp) (api.CustomerAppData, error) {
	apiCustomerAppData := api.CustomerAppData{}
	appId := a.App.GetID().ID

	switch customerAppData := a.CustomerData.(type) {
	case appstripe.CustomerData:
		stripeApp, ok := a.App.(appstripe.App)
		if !ok {
			return apiCustomerAppData, fmt.Errorf("error casting app to stripe app")
		}

		// Convert to API stripe customer app data
		apiStripeCustomerAppData := ToAPIStripeCustomerAppData(customerAppData, stripeApp)

		// Convert to API customer app data
		err := apiCustomerAppData.FromStripeCustomerAppData(apiStripeCustomerAppData)
		if err != nil {
			return apiCustomerAppData, fmt.Errorf("error converting to stripe customer app: %w", err)
		}

	case appsandbox.CustomerData:
		sandboxApp, ok := a.App.(appsandbox.App)
		if !ok {
			return apiCustomerAppData, fmt.Errorf("error casting app to sandbox app")
		}

		apiApp := mapSandboxAppToAPI(sandboxApp.Meta)

		apiSandboxCustomerAppData := api.SandboxCustomerAppData{
			Id:   &appId,
			Type: api.SandboxCustomerAppDataTypeSandbox,
			App:  &apiApp,
		}

		err := apiCustomerAppData.FromSandboxCustomerAppData(apiSandboxCustomerAppData)
		if err != nil {
			return apiCustomerAppData, fmt.Errorf("error converting to sandbox customer app: %w", err)
		}

	case appcustominvoicing.CustomerData:
		customInvoicingApp, ok := a.App.(appcustominvoicing.App)
		if !ok {
			return apiCustomerAppData, fmt.Errorf("error casting app to custom invoicing app")
		}

		apiApp := mapCustomInvoicingAppToAPI(customInvoicingApp.Meta)

		apiCustomInvoicingCustomerAppData := api.CustomInvoicingCustomerAppData{
			Id:   &appId,
			Type: api.CustomInvoicingCustomerAppDataTypeCustomInvoicing,
			App:  &apiApp,
		}

		err := apiCustomerAppData.FromCustomInvoicingCustomerAppData(apiCustomInvoicingCustomerAppData)
		if err != nil {
			return apiCustomerAppData, fmt.Errorf("error converting to custom invoicing customer app: %w", err)
		}
	default:
		return apiCustomerAppData, fmt.Errorf("unsupported customer data for app: %s", appId)
	}

	return apiCustomerAppData, nil
}