package refreshtokensdb

import (
	"context"
	"time"

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

func (db *RefreshTokenDB) RevokeAll(ctx context.Context, accountRef bson.ObjectID, deviceID string) error {
	query := repository.Query().
		Filter(repository.AccountField(), accountRef).
		And(repository.Query().Comparison(repository.Field("deviceId"), builder.Ne, deviceID)).
		And(repository.Query().Comparison(repository.Field(IsRevokedField), builder.Eq, false))

	patch := repository.Patch().
		Set(repository.Field(ExpiresAtField), time.Now()).
		Set(repository.Field(IsRevokedField), true)

	_, err := db.Repository.PatchMany(ctx, query, patch)
	return err
}
