package theme

import (
	"path/filepath"
	"testing"

	"github.com/vstockwell/wraith/internal/db"
)

func setupTestDB(t *testing.T) *ThemeService {
	t.Helper()
	d, err := db.Open(filepath.Join(t.TempDir(), "test.db"))
	if err != nil {
		t.Fatal(err)
	}
	if err := db.Migrate(d); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { d.Close() })
	return NewThemeService(d)
}

func TestSeedBuiltins(t *testing.T) {
	svc := setupTestDB(t)
	if err := svc.SeedBuiltins(); err != nil {
		t.Fatalf("SeedBuiltins() error: %v", err)
	}

	themes, err := svc.List()
	if err != nil {
		t.Fatalf("List() error: %v", err)
	}
	if len(themes) != len(BuiltinThemes) {
		t.Errorf("len(themes) = %d, want %d", len(themes), len(BuiltinThemes))
	}
}

func TestSeedBuiltinsIdempotent(t *testing.T) {
	svc := setupTestDB(t)
	svc.SeedBuiltins()
	svc.SeedBuiltins()

	themes, _ := svc.List()
	if len(themes) != len(BuiltinThemes) {
		t.Errorf("len(themes) = %d after double seed, want %d", len(themes), len(BuiltinThemes))
	}
}

func TestGetByName(t *testing.T) {
	svc := setupTestDB(t)
	svc.SeedBuiltins()

	theme, err := svc.GetByName("Dracula")
	if err != nil {
		t.Fatalf("GetByName() error: %v", err)
	}
	if theme.Background != "#282a36" {
		t.Errorf("Background = %q, want %q", theme.Background, "#282a36")
	}
}
