// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package middleware

import (
	"context"
	"time"

	"github.com/absmach/magistrala/bootstrap"
	smqauthn "github.com/absmach/magistrala/pkg/authn"
	"github.com/go-kit/kit/metrics"
)

var _ bootstrap.Service = (*metricsMiddleware)(nil)

type metricsMiddleware struct {
	counter metrics.Counter
	latency metrics.Histogram
	svc     bootstrap.Service
}

// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc bootstrap.Service, counter metrics.Counter, latency metrics.Histogram) bootstrap.Service {
	return &metricsMiddleware{
		counter: counter,
		latency: latency,
		svc:     svc,
	}
}

// Add instruments Add method with metrics.
func (mm *metricsMiddleware) Add(ctx context.Context, session smqauthn.Session, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "add").Add(1)
		mm.latency.With("method", "add").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.Add(ctx, session, token, cfg)
}

// View instruments View method with metrics.
func (mm *metricsMiddleware) View(ctx context.Context, session smqauthn.Session, id string) (saved bootstrap.Config, err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "view").Add(1)
		mm.latency.With("method", "view").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.View(ctx, session, id)
}

// Update instruments Update method with metrics.
func (mm *metricsMiddleware) Update(ctx context.Context, session smqauthn.Session, cfg bootstrap.Config) (err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "update").Add(1)
		mm.latency.With("method", "update").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.Update(ctx, session, cfg)
}

// UpdateCert instruments UpdateCert method with metrics.
func (mm *metricsMiddleware) UpdateCert(ctx context.Context, session smqauthn.Session, id, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "update_cert").Add(1)
		mm.latency.With("method", "update_cert").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.UpdateCert(ctx, session, id, clientCert, clientKey, caCert)
}

// List instruments List method with metrics.
func (mm *metricsMiddleware) List(ctx context.Context, session smqauthn.Session, filter bootstrap.Filter, offset, limit uint64) (saved bootstrap.ConfigsPage, err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "list").Add(1)
		mm.latency.With("method", "list").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.List(ctx, session, filter, offset, limit)
}

// Remove instruments Remove method with metrics.
func (mm *metricsMiddleware) Remove(ctx context.Context, session smqauthn.Session, id string) (err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "remove").Add(1)
		mm.latency.With("method", "remove").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.Remove(ctx, session, id)
}

// Bootstrap instruments Bootstrap method with metrics.
func (mm *metricsMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "bootstrap").Add(1)
		mm.latency.With("method", "bootstrap").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.Bootstrap(ctx, externalKey, externalID, secure)
}

func (mm *metricsMiddleware) EnableConfig(ctx context.Context, session smqauthn.Session, id string) (bootstrap.Config, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "enable_config").Add(1)
		mm.latency.With("method", "enable_config").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.EnableConfig(ctx, session, id)
}

func (mm *metricsMiddleware) DisableConfig(ctx context.Context, session smqauthn.Session, id string) (bootstrap.Config, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "disable_config").Add(1)
		mm.latency.With("method", "disable_config").Observe(time.Since(begin).Seconds())
	}(time.Now())

	return mm.svc.DisableConfig(ctx, session, id)
}

func (mm *metricsMiddleware) CreateProfile(ctx context.Context, session smqauthn.Session, p bootstrap.Profile) (bootstrap.Profile, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "create_profile").Add(1)
		mm.latency.With("method", "create_profile").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.CreateProfile(ctx, session, p)
}

func (mm *metricsMiddleware) ViewProfile(ctx context.Context, session smqauthn.Session, profileID string) (bootstrap.Profile, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "view_profile").Add(1)
		mm.latency.With("method", "view_profile").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.ViewProfile(ctx, session, profileID)
}

func (mm *metricsMiddleware) UpdateProfile(ctx context.Context, session smqauthn.Session, p bootstrap.Profile) (bootstrap.Profile, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "update_profile").Add(1)
		mm.latency.With("method", "update_profile").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.UpdateProfile(ctx, session, p)
}

func (mm *metricsMiddleware) ListProfiles(ctx context.Context, session smqauthn.Session, offset, limit uint64, name string) (bootstrap.ProfilesPage, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "list_profiles").Add(1)
		mm.latency.With("method", "list_profiles").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.ListProfiles(ctx, session, offset, limit, name)
}

func (mm *metricsMiddleware) DeleteProfile(ctx context.Context, session smqauthn.Session, profileID string) error {
	defer func(begin time.Time) {
		mm.counter.With("method", "delete_profile").Add(1)
		mm.latency.With("method", "delete_profile").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.DeleteProfile(ctx, session, profileID)
}

func (mm *metricsMiddleware) AssignProfile(ctx context.Context, session smqauthn.Session, configID, profileID string) error {
	defer func(begin time.Time) {
		mm.counter.With("method", "assign_profile").Add(1)
		mm.latency.With("method", "assign_profile").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.AssignProfile(ctx, session, configID, profileID)
}

func (mm *metricsMiddleware) BindResources(ctx context.Context, session smqauthn.Session, token, configID string, bindings []bootstrap.BindingRequest) error {
	defer func(begin time.Time) {
		mm.counter.With("method", "bind_resources").Add(1)
		mm.latency.With("method", "bind_resources").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.BindResources(ctx, session, token, configID, bindings)
}

func (mm *metricsMiddleware) ListBindings(ctx context.Context, session smqauthn.Session, configID string) ([]bootstrap.BindingSnapshot, error) {
	defer func(begin time.Time) {
		mm.counter.With("method", "list_bindings").Add(1)
		mm.latency.With("method", "list_bindings").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.ListBindings(ctx, session, configID)
}

func (mm *metricsMiddleware) RefreshBindings(ctx context.Context, session smqauthn.Session, token, configID string) error {
	defer func(begin time.Time) {
		mm.counter.With("method", "refresh_bindings").Add(1)
		mm.latency.With("method", "refresh_bindings").Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mm.svc.RefreshBindings(ctx, session, token, configID)
}
