package renderer_test

import (
	"strings"
	"testing"

	"git.max-richter.dev/max/marka/renderer"
	"git.max-richter.dev/max/marka/testdata"
	"github.com/google/go-cmp/cmp"
)

func TestRenderFile_RecipeSalad(t *testing.T) {
	inputJSON := testdata.Read(t, "recipe_salad/output.json")
	expectedMarkdown := testdata.Read(t, "recipe_salad/input.md")

	gotMarkdown, err := renderer.RenderFile([]byte(inputJSON))
	if err != nil {
		t.Fatalf("RenderFile failed: %v", err)
	}

	if diff := cmp.Diff(string(expectedMarkdown), string(gotMarkdown)); diff != "" {
		t.Errorf("Rendered markdown mismatch (-want +got):\n%s", diff)
	}
}

func TestRenderFile_RecipeNoDescription(t *testing.T) {
	inputJSON := testdata.Read(t, "recipe_no_description/output.json")
	expectedMarkdown := testdata.Read(t, "recipe_no_description/input.md")

	gotMarkdown, err := renderer.RenderFile([]byte(inputJSON))
	if err != nil {
		t.Fatalf("RenderFile failed: %v", err)
	}

	if diff := cmp.Diff(string(expectedMarkdown), string(gotMarkdown)); diff != "" {
		t.Errorf("Rendered markdown mismatch (-want +got):\n%s", diff)
	}
}

func TestRenderFile_MissingType(t *testing.T) {
	rawJSON := []byte(`{"name": "Test"}`)
	_, err := renderer.RenderFile(rawJSON)
	if err == nil {
		t.Fatal("expected error for missing _type, got nil")
	}
	if !strings.Contains(err.Error(), "JSON does not contain a valid '_type' property") {
		t.Errorf("expected missing _type error, got: %v", err)
	}
}

func TestRenderFile_InvalidJSON(t *testing.T) {
	rawJSON := []byte(`{"name": "Test"`)
	_, err := renderer.RenderFile(rawJSON)
	if err == nil {
		t.Fatal("expected error for invalid JSON, got nil")
	}
	if !strings.Contains(err.Error(), "failed to parse JSON") {
		t.Errorf("expected invalid JSON error, got: %v", err)
	}
}

func TestRenderFile_RecipeBaguette(t *testing.T) {
	inputJSON := testdata.Read(t, "baguette/output.json")
	expectedMarkdown := testdata.Read(t, "baguette/input.md")

	gotMarkdown, err := renderer.RenderFile(inputJSON)
	if err != nil {
		t.Fatalf("RenderFile failed: %v", err)
	}

	if diff := cmp.Diff(string(expectedMarkdown), string(gotMarkdown)); diff != "" {
		t.Errorf("Rendered markdown mismatch (-want +got):\n%s", diff)
	}
}

func TestRenderFile_ArticleSimple(t *testing.T) {
	inputJSON := testdata.Read(t, "article_simple/output.json")
	expectedMarkdown := testdata.Read(t, "article_simple/input.md")

	gotMarkdown, err := renderer.RenderFile([]byte(inputJSON))
	if err != nil {
		t.Fatalf("RenderFile failed: %v", err)
	}

	if diff := cmp.Diff(string(expectedMarkdown), string(gotMarkdown)); diff != "" {
		t.Errorf("Rendered markdown mismatch (-want +got):\n%s", diff)
	}
}
