package fs_test

import (
	"context"
	"testing"
	"testing/fstest"

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

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

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

	testLangEn := domain.NewLanguage("en")
	testLangRu := domain.NewLanguage("ru")
	testData := fstest.MapFS{
		"index.ru.md": &fstest.MapFile{
			Data: []byte("---\ntitle: пример\n---\nпривет, мир!\n"),
		},
		"index.en.md": &fstest.MapFile{
			Data: []byte("---\ntitle: example\n---\nhello, world!\n"),
		},
	}

	repo := repository.NewFileSystemSiteRepository(testData)

	for name, tc := range map[string]struct {
		expect *domain.Site
		input  domain.Language
	}{
		"english": {
			input: testLangEn,
			expect: &domain.Site{
				Language:  testLangEn,
				Title:     "example",
				Params:    make(map[string]any),
				Languages: make([]domain.Language, 0),
			},
		},
		"russian": {
			input: testLangRu,
			expect: &domain.Site{
				Language:  testLangRu,
				Title:     "пример",
				Params:    make(map[string]any),
				Languages: make([]domain.Language, 0),
			},
		},
	} {
		name, tc := name, tc

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

			out, err := repo.Get(context.Background(), 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", "DefaultLanguage":
					return true
				}
			}, cmp.Ignore())); diff != "" {
				t.Error(diff)
			}
		})
	}
}
