package httpapi

import (
	"testing"
)

func TestParseManualSource(t *testing.T) {
	cases := []struct {
		name         string
		provider     string
		raw          string
		wantProvider string
		wantID       string
		wantErr      bool
	}{
		{"id с провайдером", "tmdb", "60622", "tmdb", "60622", false},
		{"id обрезается позже воркером", "TVDB", "269613", "tvdb", "269613", false},
		{"URL TMDB со slug", "", "https://www.themoviedb.org/tv/60622-fargo", "tmdb", "60622", false},
		{"URL TMDB без схемы", "", "themoviedb.org/tv/60622", "tmdb", "60622", false},
		{"URL TMDB movie", "", "https://www.themoviedb.org/movie/693134", "tmdb", "693134", false},
		{"URL IMDb", "", "https://www.imdb.com/title/tt0111161/", "imdb", "tt0111161", false},
		{"URL TVDB dereferrer", "", "https://www.thetvdb.com/dereferrer/series/269613", "tvdb", "269613", false},
		{"URL TVDB slug — без id → ошибка", "", "https://www.thetvdb.com/series/fargo", "", "", true},
		{"мусорный URL → ошибка", "", "https://example.com/foo/bar", "", "", true},
		{"пустой ввод → ошибка", "tmdb", "  ", "", "", true},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			p, id, err := parseManualSource(c.provider, c.raw)
			if c.wantErr {
				if err == nil {
					t.Fatalf("ожидалась ошибка, получено %q:%q", p, id)
				}
				return
			}
			if err != nil {
				t.Fatalf("неожиданная ошибка: %v", err)
			}
			if p != c.wantProvider || id != c.wantID {
				t.Errorf("parseManualSource(%q,%q) = %q:%q, want %q:%q", c.provider, c.raw, p, id, c.wantProvider, c.wantID)
			}
		})
	}
}
