package images_test

import (
	"context"
	"errors"
	"testing"

	"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
	"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
	"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/test/testdata"
	"github.com/valkey-io/valkey-go"
	"github.com/valkey-io/valkey-go/mock"
	"go.uber.org/mock/gomock"
)

func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
	valkey := mock.NewClient(ctrl)
	return images.New(testdata.FS(), &config.Image{Concurency: 1, Quality: 80}, valkey), valkey
}

func BenchmarkService_GetImage(b *testing.B) {
	srv, valkeyClient := Setup(b.Context(), gomock.NewController(b))
	uid, err := images.UIDFromPath("cover.jpg")
	if err != nil {
		b.Error("could not get uid", err)
	}

	b.ReportAllocs()
	for b.Loop() {
		valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.ErrorResult(errors.New("adf")))
		valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.Result(valkey.ValkeyMessage{}))
		_, _, err = srv.GetScaledImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
		if err != nil {
			b.Error(err)
		}
	}
}
