package fs_test

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

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

	"source.toby3d.me/toby3d/home/internal/domain"
	repository "source.toby3d.me/toby3d/home/internal/entry/repository/fs"
)

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

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

	repo := repository.NewFileSystemPageRepository(testData)

	for name, tc := range map[string]struct {
		expect *domain.Entry
		input  string
	}{
		"index": {
			input: path.Join("index"),
			expect: &domain.Entry{
				Content:      []byte("index.md"),
				Params:       make(map[string]any),
				Resources:    make([]*domain.Resource, 0),
				Translations: make([]*domain.Entry, 0),
			},
		},
		"file": {
			input: path.Join("file"),
			expect: &domain.Entry{
				Content:      []byte("file.md"),
				Params:       make(map[string]any),
				Resources:    make([]*domain.Resource, 0),
				Translations: make([]*domain.Entry, 0),
			},
		},
		"folder": {
			input: path.Join("folder", "index"),
			expect: &domain.Entry{
				Content:      []byte("folder/index.md"),
				Params:       make(map[string]any),
				Resources:    make([]*domain.Resource, 0),
				Translations: make([]*domain.Entry, 0),
			},
		},
		"both-file": {
			input: path.Join("both"),
			expect: &domain.Entry{
				Content:      []byte("both.md"),
				Params:       make(map[string]any),
				Resources:    make([]*domain.Resource, 0),
				Translations: make([]*domain.Entry, 0),
			},
		},
		"both-folder": {
			input: path.Join("both", "index"),
			expect: &domain.Entry{
				Content:      []byte("both/index.md"),
				Params:       make(map[string]any),
				Resources:    make([]*domain.Resource, 0),
				Translations: make([]*domain.Entry, 0),
			},
		},
	} {
		name, tc := name, tc

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

			out, err := repo.Get(context.Background(), domain.LanguageUnd, tc.input)
			if err != nil {
				t.Fatal(err)
			}

			if diff := cmp.Diff(out, tc.expect, cmp.FilterPath(func(p cmp.Path) bool {
				switch p.String() {
				default:
					return false
				case "File", "Language":
					return true
				}
			}, cmp.Ignore())); diff != "" {
				t.Error(diff)
			}
		})
	}
}
