package usecase_test

import (
	"context"
	"path/filepath"
	"testing"
	"testing/fstest"

	"github.com/google/go-cmp/cmp"

	"source.toby3d.me/toby3d/home/internal/domain"
	pagefsrepo "source.toby3d.me/toby3d/home/internal/page/repository/fs"
	"source.toby3d.me/toby3d/home/internal/page/usecase"
	"source.toby3d.me/toby3d/home/internal/resource"
)

func TestDo(t *testing.T) {
	t.Parallel()

	pages := pagefsrepo.NewFileSystemPageRepository(fstest.MapFS{
		filepath.Join("both", "index.md"):   &fstest.MapFile{Data: []byte(`both/index.md`)},
		filepath.Join("folder", "index.md"): &fstest.MapFile{Data: []byte(`folder/index.md`)},
		filepath.Join("both.md"):            &fstest.MapFile{Data: []byte(`both.md`)},
		filepath.Join("file.md"):            &fstest.MapFile{Data: []byte(`file.md`)},
		filepath.Join("index.md"):           &fstest.MapFile{Data: []byte(`index.md`)},
	})

	ucase := usecase.NewPageUseCase(pages, resource.NewDummyRepository())

	for name, tc := range map[string]struct {
		input  string
		expect []byte
	}{
		"index":        {input: "/", expect: []byte(`index.md`)},
		"index-ext":    {input: "/index.html", expect: []byte(`index.md`)},
		"file":         {input: "/file", expect: []byte(`file.md`)},
		"file-slash":   {input: "/file/", expect: []byte(`file.md`)},
		"file-ext":     {input: "/file.html", expect: []byte(`file.md`)},
		"folder":       {input: "/folder", expect: []byte(`folder/index.md`)},
		"folder-slash": {input: "/folder/", expect: []byte(`folder/index.md`)},
		"folder-index": {input: "/folder/index.html", expect: []byte(`folder/index.md`)},
		"both":         {input: "/both", expect: []byte(`both/index.md`)},
		"both-slash":   {input: "/both/", expect: []byte(`both/index.md`)},
		"both-ext":     {input: "/both.html", expect: []byte(`both.md`)},
		"both-index":   {input: "/both/index.html", expect: []byte(`both/index.md`)},
	} {
		name, tc := name, tc

		t.Run(name, func(t *testing.T) {
			t.Parallel()

			actual, err := ucase.Do(context.Background(), domain.LanguageUnd, tc.input)
			if err != nil {
				t.Fatal(err)
			}

			if diff := cmp.Diff(string(actual.Content), string(tc.expect)); diff != "" {
				t.Error(diff)
			}
		})
	}
}
