package decoders_test

import (
	"encoding/json"
	"fmt"
	"testing"

	"git.max-richter.dev/max/marka/parser/decoders"
	"git.max-richter.dev/max/marka/parser/matcher"
	"git.max-richter.dev/max/marka/registry"
	"git.max-richter.dev/max/marka/template"
	"git.max-richter.dev/max/marka/testdata"
)

func TestParseBaguette(t *testing.T) {
	recipeMd := testdata.Read(t, "baguette/input.md")

	templateContent, err := registry.GetTemplate("Recipe")
	if err != nil {
		t.Fatalf("Err: %s", err)
	}

	blocks, err := template.CompileTemplate(templateContent)
	if err != nil {
		t.Fatalf("Err: %s", err)
	}

	matches := matcher.MatchBlocksFuzzy(string(recipeMd), blocks, 0.3)
	parsed, err := decoders.Parse(matches)
	if err != nil {
		t.Fatalf("Err: %s", err)
	}
	expected := map[string]any{
		"name":             "Baguette",
		"description":      "My favourite baguette recipe",
		"recipeIngredient": []string{"Flour", "Water", "Salt"},
		"recipeInstructions": []string{
			"Mix Flour Water and Salt",
			"Bake the bread",
		},
	}

	out, _ := json.MarshalIndent(parsed, "", " ")
	fmt.Printf("Parsed: \n%s\n", string(out))

	outMap, ok := parsed.(map[string]any)
	if !ok {
		t.Fatalf("expected parsed to be map[string]any, got %T", parsed)
	}

	for k, v := range expected {
		if fmt.Sprintf("%v", outMap[k]) != fmt.Sprintf("%v", v) {
			t.Errorf("Expected %v but got %v", v, outMap[k])
		}
	}
}
