package refreshtokensdb

import (
	"github.com/tech/sendico/pkg/db/repository"
	"github.com/tech/sendico/pkg/db/repository/builder"
	"github.com/tech/sendico/pkg/model"
	"go.mongodb.org/mongo-driver/v2/bson"
)

func filterByClientId(clientID string) builder.Query {
	return repository.Query().Comparison(repository.Field("clientId"), builder.Eq, clientID)
}

func filter(session *model.SessionIdentifier) builder.Query {
	filter := filterByClientId(session.ClientID)
	filter.And(
		repository.Query().Comparison(repository.Field("deviceId"), builder.Eq, session.DeviceID),
		repository.Query().Comparison(repository.Field(IsRevokedField), builder.Eq, false),
	)
	return filter
}

func filterByAccount(accountRef bson.ObjectID, session *model.SessionIdentifier) builder.Query {
	return filter(session).And(repository.Query().Comparison(repository.AccountField(), builder.Eq, accountRef))
}
