// 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 retrylayer

import (
	"context"
	timepkg "time"
	"time"

	"github.com/lib/pq"
	"github.com/mattermost/mattermost-server/v6/model"
    "github.com/mattermost/mattermost-server/v6/store"
    "github.com/pkg/errors"
	"github.com/go-sql-driver/mysql"
)

const mySQLDeadlockCode = uint16(1213)

type {{.Name}} struct {
	store.Store
{{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}}

func isRepeatableError(err error) bool {
	var pqErr *pq.Error
	var mysqlErr *mysql.MySQLError
	switch {
	case errors.As(errors.Cause(err), &pqErr):
		if pqErr.Code == "40001" || pqErr.Code == "40P01" {
			return true
		}
	case errors.As(errors.Cause(err), &mysqlErr):
		if mysqlErr.Number == mySQLDeadlockCode {
			return true
		}
	}
	return false
}

{{range $substoreName, $substore := .SubStores}}
{{range $index, $element := $substore.Methods}}
func (s *{{$.Name}}{{$substoreName}}Store) {{$index}}({{$element.Params | joinParamsWithTypeOutsideStore}}) {{$element.Results | joinResultsForSignature}} {
    {{if $element.Results | len | eq 0}}
        s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
    {{else}}
        {{if $element.Results | errorPresent}}
            tries := 0
            for {
                {{genResultsVars $element.Results false }} := s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
                if {{$element.Results | errorVar}} == nil {
                    return {{genResultsVars $element.Results true }}
                }
                if !isRepeatableError({{$element.Results | errorVar}}) {
                    return {{genResultsVars $element.Results false }}
                }
                tries++
                if tries >= 3 {
                    {{$element.Results | errorVar}} = errors.Wrap({{$element.Results | errorVar}}, "giving up after 3 consecutive repeatable transaction failures")
                    return {{genResultsVars $element.Results false }}
                }
                timepkg.Sleep(100 * timepkg.Millisecond)
            }
        {{else}}
            return s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
        {{end}}
    {{end}}
}
{{end}}
{{end}}

{{range $index, $element := .Methods}}
func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithTypeOutsideStore}}) {{$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) *{{.Name}} {
	newStore := {{.Name}}{
		Store: childStore,
	}
	{{range $substoreName, $substore := .SubStores}}
	newStore.{{$substoreName}}Store = &{{$.Name}}{{$substoreName}}Store{{"{"}}{{$substoreName}}Store: childStore.{{$substoreName}}(), Root: &newStore}{{end}}
	return &newStore
}
