// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

// Code generated by "make store-layers"
// DO NOT EDIT

package timerlayer

import (
	"context"
    "time"

    "github.com/mattermost/mattermost-server/v6/einterfaces"
	"github.com/mattermost/mattermost-server/v6/model"
	"github.com/mattermost/mattermost-server/v6/store"
)

type {{.Name}} struct {
	store.Store
	Metrics einterfaces.MetricsInterface
{{range $index, $element := .SubStores}}	{{$index}}Store store.{{$index}}Store
{{end}}
}

{{range $index, $element := .SubStores}}func (s *{{$.Name}}) {{$index}}() store.{{$index}}Store {
	return s.{{$index}}Store
}

{{end}}

{{range $index, $element := .SubStores}}type {{$.Name}}{{$index}}Store struct {
	store.{{$index}}Store
	Root *{{$.Name}}
}

{{end}}

{{range $substoreName, $substore := .SubStores}}
{{range $index, $element := $substore.Methods}}
func (s *{{$.Name}}{{$substoreName}}Store) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	start := time.Now()
	{{if $element.Results | len | eq 0}}
	s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
	{{else}}
	{{genResultsVars $element.Results false }} := s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
	{{end}}
	elapsed := float64(time.Since(start)) / float64(time.Second)
	if s.Root.Metrics != nil {
		success := "false"
		if {{$element.Results | errorToBoolean}} {
			success = "true"
		}
		s.Root.Metrics.ObserveStoreMethodDuration("{{$substoreName}}Store.{{$index}}", success, elapsed)
	{{ with (genResultsVars $element.Results false ) -}}
	}
	return {{ . }}
	{{- else -}}
	}
	{{- end }}
}
{{end}}
{{end}}

{{range $index, $element := .Methods}}
func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	{{if $element.Results | len | eq 0}}s.Store.{{$index}}({{$element.Params | joinParams}})
	{{else}}return s.Store.{{$index}}({{$element.Params | joinParams}})
	{{end}}}
{{end}}

func New(childStore store.Store, metrics einterfaces.MetricsInterface) *{{.Name}} {
	newStore := {{.Name}}{
		Store: childStore,
		Metrics: metrics,
	}
	{{range $substoreName, $substore := .SubStores}}
	newStore.{{$substoreName}}Store = &{{$.Name}}{{$substoreName}}Store{{"{"}}{{$substoreName}}Store: childStore.{{$substoreName}}(), Root: &newStore}{{end}}
	return &newStore
}
