package config

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

func TestInitAndGet_Defaults(t *testing.T) {
	// Use temp XDG to force a fresh config
	dir := t.TempDir()
	os.Setenv("XDG_CONFIG_HOME", dir)
	defer os.Unsetenv("XDG_CONFIG_HOME")

	if err := Init(); err != nil {
		t.Fatalf("init: %v", err)
	}

	if p := MustGet("player.command"); p != "mpv" {
		t.Errorf("expected mpv default, got %s", p)
	}

	if api := API(); api == "" {
		t.Error("API should have default")
	}

	if max := MaxItems(); max != 9999 {
		t.Errorf("expected 9999 default, got %d", max)
	}
}

func TestGet_Missing(t *testing.T) {
	dir := t.TempDir()
	os.Setenv("XDG_CONFIG_HOME", dir)
	defer os.Unsetenv("XDG_CONFIG_HOME")

	_ = Init()

	_, err := Get("nonexistent_key_xyz")
	if err == nil {
		t.Error("expected error for missing key")
	}
}

func TestPath(t *testing.T) {
	dir := t.TempDir()
	os.Setenv("XDG_CONFIG_HOME", dir)
	defer os.Unsetenv("XDG_CONFIG_HOME")

	_ = Init()
	p := Path()
	if p == "" || !filepath.IsAbs(p) {
		t.Errorf("expected absolute config path, got %q", p)
	}
}
