// Package svix this file is @generated DO NOT EDIT
package svix

import (
	"context"

	"github.com/svix/svix-webhooks/go/internal"
	"github.com/svix/svix-webhooks/go/models"
)

type Integration struct {
	client *internal.SvixHttpClient
}

func newIntegration(client *internal.SvixHttpClient) Integration {
	return Integration{client}
}

type IntegrationListOptions struct {
	// Limit the number of returned items
	Limit *uint64
	// The iterator returned from a prior invocation
	Iterator *string

	// The sorting order of the returned items
	Order *models.Ordering
}

type IntegrationCreateOptions struct {
	IdempotencyKey *string
}

type IntegrationRotateKeyOptions struct {
	IdempotencyKey *string
}

// List the application's integrations.
func (integration Integration) List(
	ctx context.Context,
	appId string,
	o *IntegrationListOptions,
) (*models.ListResponseIntegrationOut, error) {
	var err error
	pathMap := map[string]string{
		"app_id": appId,
	}
	queryMap := map[string]string{}
	if o == nil {
		opts := IntegrationListOptions{}
		o = &opts
	}
	internal.SerializeParamToMap("limit", o.Limit, queryMap, &err)
	internal.SerializeParamToMap("iterator", o.Iterator, queryMap, &err)
	internal.SerializeParamToMap("order", o.Order, queryMap, &err)
	if err != nil {
		return nil, err
	}
	return internal.ExecuteRequest[any, models.ListResponseIntegrationOut](
		ctx,
		integration.client,
		"GET",
		"/api/v1/app/{app_id}/integration",
		pathMap,
		queryMap,
		nil,
		nil,
	)
}

// Create an integration.
func (integration Integration) Create(
	ctx context.Context,
	appId string,
	integrationIn models.IntegrationIn,
	o *IntegrationCreateOptions,
) (*models.IntegrationOut, error) {
	var err error
	pathMap := map[string]string{
		"app_id": appId,
	}
	headerMap := map[string]string{}
	if o == nil {
		opts := IntegrationCreateOptions{}
		o = &opts
	}
	internal.SerializeParamToMap("idempotency-key", o.IdempotencyKey, headerMap, &err)
	if err != nil {
		return nil, err
	}
	return internal.ExecuteRequest[models.IntegrationIn, models.IntegrationOut](
		ctx,
		integration.client,
		"POST",
		"/api/v1/app/{app_id}/integration",
		pathMap,
		nil,
		headerMap,
		&integrationIn,
	)
}

// Get an integration.
func (integration Integration) Get(
	ctx context.Context,
	appId string,
	integId string,
) (*models.IntegrationOut, error) {
	pathMap := map[string]string{
		"app_id":   appId,
		"integ_id": integId,
	}
	return internal.ExecuteRequest[any, models.IntegrationOut](
		ctx,
		integration.client,
		"GET",
		"/api/v1/app/{app_id}/integration/{integ_id}",
		pathMap,
		nil,
		nil,
		nil,
	)
}

// Update an integration.
func (integration Integration) Update(
	ctx context.Context,
	appId string,
	integId string,
	integrationUpdate models.IntegrationUpdate,
) (*models.IntegrationOut, error) {
	pathMap := map[string]string{
		"app_id":   appId,
		"integ_id": integId,
	}
	return internal.ExecuteRequest[models.IntegrationUpdate, models.IntegrationOut](
		ctx,
		integration.client,
		"PUT",
		"/api/v1/app/{app_id}/integration/{integ_id}",
		pathMap,
		nil,
		nil,
		&integrationUpdate,
	)
}

// Delete an integration.
func (integration Integration) Delete(
	ctx context.Context,
	appId string,
	integId string,
) error {
	var err error
	pathMap := map[string]string{
		"app_id":   appId,
		"integ_id": integId,
	}
	_, err = internal.ExecuteRequest[any, any](
		ctx,
		integration.client,
		"DELETE",
		"/api/v1/app/{app_id}/integration/{integ_id}",
		pathMap,
		nil,
		nil,
		nil,
	)
	return err
}

// Rotate the integration's key. The previous key will be immediately revoked.
func (integration Integration) RotateKey(
	ctx context.Context,
	appId string,
	integId string,
	o *IntegrationRotateKeyOptions,
) (*models.IntegrationKeyOut, error) {
	var err error
	pathMap := map[string]string{
		"app_id":   appId,
		"integ_id": integId,
	}
	headerMap := map[string]string{}
	if o == nil {
		opts := IntegrationRotateKeyOptions{}
		o = &opts
	}
	internal.SerializeParamToMap("idempotency-key", o.IdempotencyKey, headerMap, &err)
	if err != nil {
		return nil, err
	}
	return internal.ExecuteRequest[any, models.IntegrationKeyOut](
		ctx,
		integration.client,
		"POST",
		"/api/v1/app/{app_id}/integration/{integ_id}/key/rotate",
		pathMap,
		nil,
		headerMap,
		nil,
	)
}
