package tagfile

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

	"axenov/iptv-checker/app/playlist"
)

func TestGetTags_TvgId(t *testing.T) {
	block := TagBlock{
		TvgId: "^ru-",
		Tags:  []string{"russian"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{"tvg-id": "ru-first"},
	}
	tags := block.GetTags(ch)
	if len(tags) != 1 || tags[0] != "russian" {
		t.Errorf("expected [russian], got %v", tags)
	}
}

func TestGetTags_TvgId_NoMatch(t *testing.T) {
	block := TagBlock{
		TvgId: "^en-",
		Tags:  []string{"english"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{"tvg-id": "ru-first"},
	}
	tags := block.GetTags(ch)
	if len(tags) != 0 {
		t.Errorf("expected empty, got %v", tags)
	}
}

func TestGetTags_TvgId_NoAttribute(t *testing.T) {
	block := TagBlock{
		TvgId: "^ru-",
		Tags:  []string{"russian"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{},
	}
	tags := block.GetTags(ch)
	if len(tags) != 0 {
		t.Errorf("expected empty, got %v", tags)
	}
}

func TestGetTags_TvgName(t *testing.T) {
	block := TagBlock{
		TvgName: "sport",
		Tags:    []string{"sports"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{"tvg-name": "Sports Channel"},
	}
	tags := block.GetTags(ch)
	if len(tags) != 1 || tags[0] != "sports" {
		t.Errorf("expected [sports], got %v", tags)
	}
}

func TestGetTags_Title(t *testing.T) {
	block := TagBlock{
		Title: "movie",
		Tags:  []string{"movies"},
	}
	ch := playlist.Channel{
		Title: "Best Movie HD",
	}
	tags := block.GetTags(ch)
	if len(tags) != 1 || tags[0] != "movies" {
		t.Errorf("expected [movies], got %v", tags)
	}
}

func TestGetTags_TitleCaseInsensitive(t *testing.T) {
	block := TagBlock{
		Title: "hd",
		Tags:  []string{"hd"},
	}
	ch := playlist.Channel{
		Title: "some HD channel",
	}
	tags := block.GetTags(ch)
	if len(tags) != 1 || tags[0] != "hd" {
		t.Errorf("expected [hd], got %v", tags)
	}
}

func TestGetTags_EmptyBlock(t *testing.T) {
	block := TagBlock{}
	ch := playlist.Channel{
		Title:      "Test",
		Attributes: map[string]string{},
	}
	tags := block.GetTags(ch)
	if len(tags) != 0 {
		t.Errorf("expected empty, got %v", tags)
	}
}

func TestGetTags_InvalidRegex(t *testing.T) {
	block := TagBlock{
		TvgId: "[invalid",
		Tags:  []string{"bad"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{"tvg-id": "test"},
	}
	tags := block.GetTags(ch)
	if len(tags) != 0 {
		t.Errorf("expected empty for invalid regex, got %v", tags)
	}
}

func TestGetTags_TvgIdEmptyValue(t *testing.T) {
	block := TagBlock{
		TvgId: "test",
		Tags:  []string{"tag"},
	}
	ch := playlist.Channel{
		Attributes: map[string]string{"tvg-id": ""},
	}
	tags := block.GetTags(ch)
	if len(tags) != 0 {
		t.Errorf("expected empty for empty tvg-id value, got %v", tags)
	}
}

func TestInit_Success(t *testing.T) {
	tmp := t.TempDir()
	path := filepath.Join(tmp, "channels.json")
	data := `[{"tvg-id":"^ru-","tags":["russian"]},{"title":"movie","tags":["movies"]}]`
	if err := os.WriteFile(path, []byte(data), 0644); err != nil {
		t.Fatal(err)
	}
	blocks := Init(path)
	if len(blocks) != 2 {
		t.Fatalf("expected 2 blocks, got %d", len(blocks))
	}
}

func TestInit_NotFound(t *testing.T) {
	blocks := Init("/nonexistent/channels.json")
	if blocks != nil {
		t.Errorf("expected nil for non-existent file, got %v", blocks)
	}
}

func TestInit_InvalidJSON(t *testing.T) {
	tmp := t.TempDir()
	path := filepath.Join(tmp, "channels.json")
	os.WriteFile(path, []byte("not json"), 0644)
	blocks := Init(path)
	if blocks != nil {
		t.Errorf("expected nil for invalid JSON, got %v", blocks)
	}
}
