package subsonic

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

	"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
)

func TestFindSidecarCover(t *testing.T) {
	dir := t.TempDir()
	track := filepath.Join(dir, "01 - song.mp3")
	if err := os.WriteFile(track, []byte("audio"), 0o644); err != nil {
		t.Fatalf("write track: %v", err)
	}

	// Empty dir first: no cover.
	if got := coverart.FindSidecar(filepath.Dir(track)); got != "" {
		t.Errorf("no sidecar → %q, want empty", got)
	}

	// folder.jpg lower priority than cover.jpg: add folder first, expect hit;
	// then add cover.jpg, expect cover to win.
	folder := filepath.Join(dir, "folder.jpg")
	if err := os.WriteFile(folder, []byte("jpg"), 0o644); err != nil {
		t.Fatal(err)
	}
	if got := coverart.FindSidecar(filepath.Dir(track)); got != folder {
		t.Errorf("folder.jpg fallback = %q, want %q", got, folder)
	}
	cover := filepath.Join(dir, "cover.jpg")
	if err := os.WriteFile(cover, []byte("jpg"), 0o644); err != nil {
		t.Fatal(err)
	}
	if got := coverart.FindSidecar(filepath.Dir(track)); got != cover {
		t.Errorf("cover.jpg priority = %q, want %q", got, cover)
	}
}

func TestImageContentType(t *testing.T) {
	cases := map[string]string{
		"cover.jpg":    "image/jpeg",
		"cover.JPEG":   "image/jpeg",
		"art.png":      "image/png",
		"thumb.gif":    "image/gif",
		"square.webp":  "image/webp",
		"unknown.tiff": "application/octet-stream",
	}
	for in, want := range cases {
		if got := imageContentType(in); got != want {
			t.Errorf("imageContentType(%q) = %q, want %q", in, got, want)
		}
	}
}
