package server

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"mkblog/internal/site"
)

func TestDiffWatchState(t *testing.T) {
	now := time.Unix(100, 0)
	later := now.Add(time.Second)

	prev := map[string]time.Time{
		"/repo/content/keep.md":  now,
		"/repo/content/old.md":   now,
		"/repo/themes/theme.css": now,
		"/repo/static/script.js": now,
		"/repo/static/unchanged": now,
	}
	next := map[string]time.Time{
		"/repo/content/keep.md":  later,
		"/repo/themes/theme.css": now,
		"/repo/static/new.js":    now,
		"/repo/static/unchanged": now,
	}

	got := diffWatchState(prev, next)
	want := []watchChange{
		{Kind: "modified", Path: "/repo/content/keep.md"},
		{Kind: "removed", Path: "/repo/content/old.md"},
		{Kind: "added", Path: "/repo/static/new.js"},
		{Kind: "removed", Path: "/repo/static/script.js"},
	}

	if len(got) != len(want) {
		t.Fatalf("diffWatchState len = %d, want %d; got %#v", len(got), len(want), got)
	}
	for i := range want {
		if got[i] != want[i] {
			t.Fatalf("diffWatchState[%d] = %#v, want %#v", i, got[i], want[i])
		}
	}
}

func TestSummarizeWatchChanges(t *testing.T) {
	changes := []watchChange{
		{Kind: "modified", Path: "content/hello.md"},
		{Kind: "added", Path: "themes/tt-jit-wrap/assets/new.css"},
		{Kind: "removed", Path: "static/old.js"},
	}

	got := summarizeWatchChanges(changes, 2)

	checks := []string{
		"3 changes:",
		"modified content/hello.md",
		"added themes/tt-jit-wrap/assets/new.css",
		"... and 1 more",
	}
	for _, check := range checks {
		if !strings.Contains(got, check) {
			t.Fatalf("summary %q missing %q", got, check)
		}
	}
	if strings.Contains(got, "removed static/old.js") {
		t.Fatalf("summary %q should respect limit", got)
	}
}

func TestSummarizeWatchChangesSingular(t *testing.T) {
	got := summarizeWatchChanges([]watchChange{{Kind: "modified", Path: "content/one.md"}}, 5)
	if got != "1 change: modified content/one.md" {
		t.Fatalf("summary = %q", got)
	}
}

func TestWatchPathsUsesConfigTheme(t *testing.T) {
	dir := t.TempDir()
	configPath := filepath.Join(dir, "config.yaml")
	if err := os.WriteFile(configPath, []byte("title: Test Site\ntheme: config-theme\n"), 0o644); err != nil {
		t.Fatalf("write config: %v", err)
	}

	paths, err := watchPaths(site.BuildOptions{
		ConfigPath: configPath,
		ContentDir: filepath.Join(dir, "content"),
		StaticDir:  filepath.Join(dir, "static"),
		ThemesDir:  filepath.Join(dir, "themes"),
	})
	if err != nil {
		t.Fatalf("watchPaths error: %v", err)
	}

	wantTheme := filepath.Join(dir, "themes", "config-theme")
	if paths[len(paths)-1] != wantTheme {
		t.Fatalf("theme path = %q, want %q", paths[len(paths)-1], wantTheme)
	}
}

func TestWatchPathsUsesThemeOverride(t *testing.T) {
	dir := t.TempDir()
	configPath := filepath.Join(dir, "config.yaml")
	if err := os.WriteFile(configPath, []byte("title: Test Site\ntheme: config-theme\n"), 0o644); err != nil {
		t.Fatalf("write config: %v", err)
	}

	paths, err := watchPaths(site.BuildOptions{
		ConfigPath:    configPath,
		ContentDir:    filepath.Join(dir, "content"),
		StaticDir:     filepath.Join(dir, "static"),
		ThemesDir:     filepath.Join(dir, "themes"),
		ThemeOverride: "override-theme",
	})
	if err != nil {
		t.Fatalf("watchPaths error: %v", err)
	}

	wantTheme := filepath.Join(dir, "themes", "override-theme")
	if paths[len(paths)-1] != wantTheme {
		t.Fatalf("theme path = %q, want %q", paths[len(paths)-1], wantTheme)
	}
}

func TestWaitForWatchBurst(t *testing.T) {
	events := make(chan struct{}, 3)
	events <- struct{}{}
	events <- struct{}{}
	events <- struct{}{}

	if !waitForWatchBurst(events, 5*time.Millisecond) {
		t.Fatalf("waitForWatchBurst = false, want true")
	}
}
