// Code generated by protoc-gen-go-cosmos-orm. DO NOT EDIT.

package macaroonv1

import (
	context "context"
	ormlist "cosmossdk.io/orm/model/ormlist"
	ormtable "cosmossdk.io/orm/model/ormtable"
	ormerrors "cosmossdk.io/orm/types/ormerrors"
)

type GrantTable interface {
	Insert(ctx context.Context, grant *Grant) error
	InsertReturningId(ctx context.Context, grant *Grant) (uint64, error)
	LastInsertedSequence(ctx context.Context) (uint64, error)
	Update(ctx context.Context, grant *Grant) error
	Save(ctx context.Context, grant *Grant) error
	Delete(ctx context.Context, grant *Grant) error
	Has(ctx context.Context, id uint64) (found bool, err error)
	// Get returns nil and an error which responds true to ormerrors.IsNotFound() if the record was not found.
	Get(ctx context.Context, id uint64) (*Grant, error)
	HasBySubjectOrigin(ctx context.Context, subject string, origin string) (found bool, err error)
	// GetBySubjectOrigin returns nil and an error which responds true to ormerrors.IsNotFound() if the record was not found.
	GetBySubjectOrigin(ctx context.Context, subject string, origin string) (*Grant, error)
	List(ctx context.Context, prefixKey GrantIndexKey, opts ...ormlist.Option) (GrantIterator, error)
	ListRange(ctx context.Context, from, to GrantIndexKey, opts ...ormlist.Option) (GrantIterator, error)
	DeleteBy(ctx context.Context, prefixKey GrantIndexKey) error
	DeleteRange(ctx context.Context, from, to GrantIndexKey) error

	doNotImplement()
}

type GrantIterator struct {
	ormtable.Iterator
}

func (i GrantIterator) Value() (*Grant, error) {
	var grant Grant
	err := i.UnmarshalMessage(&grant)
	return &grant, err
}

type GrantIndexKey interface {
	id() uint32
	values() []interface{}
	grantIndexKey()
}

// primary key starting index..
type GrantPrimaryKey = GrantIdIndexKey

type GrantIdIndexKey struct {
	vs []interface{}
}

func (x GrantIdIndexKey) id() uint32            { return 0 }
func (x GrantIdIndexKey) values() []interface{} { return x.vs }
func (x GrantIdIndexKey) grantIndexKey()        {}

func (this GrantIdIndexKey) WithId(id uint64) GrantIdIndexKey {
	this.vs = []interface{}{id}
	return this
}

type GrantSubjectOriginIndexKey struct {
	vs []interface{}
}

func (x GrantSubjectOriginIndexKey) id() uint32            { return 1 }
func (x GrantSubjectOriginIndexKey) values() []interface{} { return x.vs }
func (x GrantSubjectOriginIndexKey) grantIndexKey()        {}

func (this GrantSubjectOriginIndexKey) WithSubject(subject string) GrantSubjectOriginIndexKey {
	this.vs = []interface{}{subject}
	return this
}

func (this GrantSubjectOriginIndexKey) WithSubjectOrigin(subject string, origin string) GrantSubjectOriginIndexKey {
	this.vs = []interface{}{subject, origin}
	return this
}

type grantTable struct {
	table ormtable.AutoIncrementTable
}

func (this grantTable) Insert(ctx context.Context, grant *Grant) error {
	return this.table.Insert(ctx, grant)
}

func (this grantTable) Update(ctx context.Context, grant *Grant) error {
	return this.table.Update(ctx, grant)
}

func (this grantTable) Save(ctx context.Context, grant *Grant) error {
	return this.table.Save(ctx, grant)
}

func (this grantTable) Delete(ctx context.Context, grant *Grant) error {
	return this.table.Delete(ctx, grant)
}

func (this grantTable) InsertReturningId(ctx context.Context, grant *Grant) (uint64, error) {
	return this.table.InsertReturningPKey(ctx, grant)
}

func (this grantTable) LastInsertedSequence(ctx context.Context) (uint64, error) {
	return this.table.LastInsertedSequence(ctx)
}

func (this grantTable) Has(ctx context.Context, id uint64) (found bool, err error) {
	return this.table.PrimaryKey().Has(ctx, id)
}

func (this grantTable) Get(ctx context.Context, id uint64) (*Grant, error) {
	var grant Grant
	found, err := this.table.PrimaryKey().Get(ctx, &grant, id)
	if err != nil {
		return nil, err
	}
	if !found {
		return nil, ormerrors.NotFound
	}
	return &grant, nil
}

func (this grantTable) HasBySubjectOrigin(ctx context.Context, subject string, origin string) (found bool, err error) {
	return this.table.GetIndexByID(1).(ormtable.UniqueIndex).Has(ctx,
		subject,
		origin,
	)
}

func (this grantTable) GetBySubjectOrigin(ctx context.Context, subject string, origin string) (*Grant, error) {
	var grant Grant
	found, err := this.table.GetIndexByID(1).(ormtable.UniqueIndex).Get(ctx, &grant,
		subject,
		origin,
	)
	if err != nil {
		return nil, err
	}
	if !found {
		return nil, ormerrors.NotFound
	}
	return &grant, nil
}

func (this grantTable) List(ctx context.Context, prefixKey GrantIndexKey, opts ...ormlist.Option) (GrantIterator, error) {
	it, err := this.table.GetIndexByID(prefixKey.id()).List(ctx, prefixKey.values(), opts...)
	return GrantIterator{it}, err
}

func (this grantTable) ListRange(ctx context.Context, from, to GrantIndexKey, opts ...ormlist.Option) (GrantIterator, error) {
	it, err := this.table.GetIndexByID(from.id()).ListRange(ctx, from.values(), to.values(), opts...)
	return GrantIterator{it}, err
}

func (this grantTable) DeleteBy(ctx context.Context, prefixKey GrantIndexKey) error {
	return this.table.GetIndexByID(prefixKey.id()).DeleteBy(ctx, prefixKey.values()...)
}

func (this grantTable) DeleteRange(ctx context.Context, from, to GrantIndexKey) error {
	return this.table.GetIndexByID(from.id()).DeleteRange(ctx, from.values(), to.values())
}

func (this grantTable) doNotImplement() {}

var _ GrantTable = grantTable{}

func NewGrantTable(db ormtable.Schema) (GrantTable, error) {
	table := db.GetTable(&Grant{})
	if table == nil {
		return nil, ormerrors.TableNotFound.Wrap(string((&Grant{}).ProtoReflect().Descriptor().FullName()))
	}
	return grantTable{table.(ormtable.AutoIncrementTable)}, nil
}

type StateStore interface {
	GrantTable() GrantTable

	doNotImplement()
}

type stateStore struct {
	grant GrantTable
}

func (x stateStore) GrantTable() GrantTable {
	return x.grant
}

func (stateStore) doNotImplement() {}

var _ StateStore = stateStore{}

func NewStateStore(db ormtable.Schema) (StateStore, error) {
	grantTable, err := NewGrantTable(db)
	if err != nil {
		return nil, err
	}

	return stateStore{
		grantTable,
	}, nil
}
