package service

import (
	"context"
	"time"

	"github.com/fleetdm/fleet/v4/server/contexts/viewer"
	"github.com/fleetdm/fleet/v4/server/fleet"
)

func (mw loggingMiddleware) GetQuerySpec(ctx context.Context, name string) (spec *fleet.QuerySpec, err error) {
	defer func(begin time.Time) {
		_ = mw.loggerDebug(err).Log(
			"method", "GetQuerySpec",
			"err", err,
			"took", time.Since(begin),
		)
	}(time.Now())
	spec, err = mw.Service.GetQuerySpec(ctx, name)
	return spec, err
}

func (mw loggingMiddleware) GetQuerySpecs(ctx context.Context) (specs []*fleet.QuerySpec, err error) {
	defer func(begin time.Time) {
		_ = mw.loggerDebug(err).Log(
			"method", "GetQuerySpecs",
			"err", err,
			"took", time.Since(begin),
		)
	}(time.Now())
	specs, err = mw.Service.GetQuerySpecs(ctx)
	return specs, err
}

func (mw loggingMiddleware) ApplyQuerySpecs(ctx context.Context, specs []*fleet.QuerySpec) (err error) {
	defer func(begin time.Time) {
		_ = mw.loggerDebug(err).Log(
			"method", "ApplyQuerySpecs",
			"err", err,
			"took", time.Since(begin),
		)
	}(time.Now())
	err = mw.Service.ApplyQuerySpecs(ctx, specs)
	return err
}

func (mw loggingMiddleware) ListQueries(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Query, error) {
	var (
		loggedInUser = "unauthenticated"
		err          error
	)
	if vc, ok := viewer.FromContext(ctx); ok {
		loggedInUser = vc.Email()
	}
	defer func(begin time.Time) {
		_ = mw.loggerDebug(err).Log(
			"method", "ListQueries",
			"err", err,
			"user", loggedInUser,
			"took", time.Since(begin),
		)
	}(time.Now())
	query, err := mw.Service.ListQueries(ctx, opt)
	return query, err
}
func (mw loggingMiddleware) GetQuery(ctx context.Context, id uint) (*fleet.Query, error) {
	var (
		loggedInUser = "unauthenticated"
		err          error
	)
	if vc, ok := viewer.FromContext(ctx); ok {

		loggedInUser = vc.Email()
	}
	defer func(begin time.Time) {
		_ = mw.loggerDebug(err).Log(
			"method", "GetQuery",
			"err", err,
			"user", loggedInUser,
			"took", time.Since(begin),
		)
	}(time.Now())
	query, err := mw.Service.GetQuery(ctx, id)
	return query, err
}
func (mw loggingMiddleware) NewQuery(ctx context.Context, p fleet.QueryPayload) (*fleet.Query, error) {
	var (
		query        *fleet.Query
		loggedInUser = "unauthenticated"
		err          error
	)
	if vc, ok := viewer.FromContext(ctx); ok {

		loggedInUser = vc.Email()
	}
	defer func(begin time.Time) {
		if query == nil {
			_ = mw.loggerInfo(err).Log(
				"method", "NewQuery",
				"err", err,
				"user", loggedInUser,
				"took", time.Since(begin),
			)
			return
		}
		_ = mw.loggerInfo(err).Log(
			"method", "NewQuery",
			"err", err,
			"user", loggedInUser,
			"name", query.Name,
			"sql", query.Query,
			"took", time.Since(begin),
		)
	}(time.Now())
	query, err = mw.Service.NewQuery(ctx, p)
	return query, err
}
func (mw loggingMiddleware) ModifyQuery(ctx context.Context, id uint, p fleet.QueryPayload) (*fleet.Query, error) {
	var (
		query        *fleet.Query
		loggedInUser = "unauthenticated"
		err          error
	)
	if vc, ok := viewer.FromContext(ctx); ok {

		loggedInUser = vc.Email()
	}
	defer func(begin time.Time) {
		if query == nil {
			_ = mw.loggerInfo(err).Log(
				"method", "ModifyQuery",
				"err", err,
				"user", loggedInUser,
				"took", time.Since(begin),
			)
			return
		}
		_ = mw.loggerInfo(err).Log(
			"method", "ModifyQuery",
			"err", err,
			"user", loggedInUser,
			"name", query.Name,
			"sql", query.Query,
			"took", time.Since(begin),
		)
	}(time.Now())
	query, err = mw.Service.ModifyQuery(ctx, id, p)
	return query, err
}
func (mw loggingMiddleware) DeleteQuery(ctx context.Context, name string) error {
	var (
		loggedInUser = "unauthenticated"
		err          error
	)
	if vc, ok := viewer.FromContext(ctx); ok {

		loggedInUser = vc.Email()
	}
	defer func(begin time.Time) {
		_ = mw.loggerInfo(err).Log(
			"method", "DeleteQuery",
			"err", err,
			"name", name,
			"user", loggedInUser,
			"took", time.Since(begin),
		)
	}(time.Now())
	err = mw.Service.DeleteQuery(ctx, name)
	return err
}
