package main

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func TestTemplateParsing(t *testing.T) {
	templateDir := projectPath("templates")
	engine, err := NewTemplateEngine(templateDir)
	if err != nil {
		t.Fatalf("Failed to create template engine: %v", err)
	}

	templates := []string{
		"modern.gohtml", "glassmorphism.gohtml", "pinterest.gohtml",
		"gallery.gohtml", "cyberpunk.gohtml", "bento.gohtml",
		"aurora.gohtml", "brutalist.gohtml", "classic-magazine.gohtml",
		"crimson.gohtml", "dark-stone.gohtml", "dev-docs.gohtml",
		"flat-modern.gohtml", "generatepress-orange.gohtml",
		"high-contrast.gohtml", "monochrome.gohtml", "ocean.gohtml",
		"premium-magazine.gohtml", "sky-blue.gohtml", "sunset.gohtml",
		"vivid-contrast.gohtml", "index.gohtml", "article.gohtml",
		"layout.gohtml",
	}

	for _, tpl := range templates {
		t.Run(tpl, func(t *testing.T) {
			tplPath := filepath.Join(templateDir, tpl)
			if _, err := os.Stat(tplPath); os.IsNotExist(err) {
				t.Skipf("Template %s not found", tpl)
			}
			if engine.templates.Lookup(tpl) == nil {
				t.Errorf("Template %s not found in parsed templates", tpl)
			}
		})
	}

	fmt.Println("All templates parsed successfully!")
}

func TestRenderArticle(t *testing.T) {
	templateDir := projectPath("templates")
	engine, err := NewTemplateEngine(templateDir)
	if err != nil {
		t.Fatalf("Failed to create template engine: %v", err)
	}

	data := ArticleData{
		Title:       "Test Article",
		Description: "Test description",
		Canonical:   "https://example.com/test.html",
		Schema:      `{"@context":"https://schema.org","@type":"Article"}`,
		SiteName:    "Test Ideas",
		AuthorName:  "Author",
		Date:        "Jan 01, 2026",
		CreatedAt:   "2026-01-01 00:00:00",
		AIBlocks:    []string{"<p>Hello world</p>"},
		Images: []Image{
			{ImageURL: "https://example.com/img1.jpg", Title: "Image 1"},
		},
		MainImage: &Image{ImageURL: "https://example.com/img1.jpg", Title: "Image 1"},
		RelatedArticles: []ArticleMeta{
			{Slug: "test-2", Title: "Test 2", Keyword: "Test 2"},
		},
		InjectHeader: "<script>console.log('header')</script>",
		InjectFooter: "<script>console.log('footer')</script>",
		InjectAds:    "",
		HasAIBlocks:  true,
		HasRelated:   true,
		HeaderInject: "<script>console.log('header')</script>",
		FooterInject: "<script>console.log('footer')</script>",
	}

	templates := []string{
		"modern.gohtml", "glassmorphism.gohtml", "pinterest.gohtml",
		"gallery.gohtml", "cyberpunk.gohtml", "bento.gohtml",
		"aurora.gohtml", "brutalist.gohtml", "classic-magazine.gohtml",
		"crimson.gohtml", "dark-stone.gohtml", "dev-docs.gohtml",
		"flat-modern.gohtml", "generatepress-orange.gohtml",
		"high-contrast.gohtml", "monochrome.gohtml", "ocean.gohtml",
		"premium-magazine.gohtml", "sky-blue.gohtml", "sunset.gohtml",
		"vivid-contrast.gohtml",
	}

	for _, tpl := range templates {
		t.Run(tpl, func(t *testing.T) {
			result, err := engine.Render(tpl, data)
			if err != nil {
				t.Errorf("Failed to render %s: %v", tpl, err)
				return
			}
			if len(result) == 0 {
				t.Errorf("Empty output for %s", tpl)
			}
			fmt.Printf("  %s: %d bytes\n", tpl, len(result))
		})
	}
}

func TestRenderIndex(t *testing.T) {
	templateDir := projectPath("templates")
	engine, err := NewTemplateEngine(templateDir)
	if err != nil {
		t.Fatalf("Failed to create template engine: %v", err)
	}

	data := IndexData{
		SiteName:    "Test Ideas",
		AuthorName:  "Author",
		Description: "Test description",
		Canonical:   "https://example.com/index.html",
		Schema:      `{"@context":"https://schema.org"}`,
		Articles: []ArticleMeta{
			{Slug: "test-1", Title: "Test 1", Keyword: "Test 1"},
			{Slug: "test-2", Title: "Test 2", Keyword: "Test 2"},
		},
		InjectHeader: "",
		InjectFooter: "",
	}

	result, err := engine.Render("index.gohtml", data)
	if err != nil {
		t.Fatalf("Failed to render index: %v", err)
	}
	if len(result) == 0 {
		t.Fatal("Empty output for index")
	}
	fmt.Printf("  index.gohtml: %d bytes\n", len(result))
}
