package catalog

import (
	"testing"

	"github.com/google/uuid"
)

func TestDiscoverFilters(t *testing.T) {
	svc := NewService()

	sections := svc.Discover(DiscoverParams{
		Page:      1,
		PageSize:  3,
		Genre:     "Sci-Fi",
		MediaType: "movie",
	})

	if len(sections) == 0 {
		t.Fatalf("expected non-empty discover sections")
	}

	for _, section := range sections {
		for _, item := range section.Items {
			if item.Type != MediaTypeMovie {
				t.Fatalf("expected movie item, got %s", item.Type)
			}
		}
	}
}

func TestDiscoverSupportsGames(t *testing.T) {
	svc := NewService()

	sections := svc.Discover(DiscoverParams{
		Page:      1,
		PageSize:  4,
		MediaType: "game",
	})

	if len(sections) == 0 {
		t.Fatalf("expected game sections")
	}

	for _, section := range sections {
		for _, item := range section.Items {
			if item.Type != MediaTypeGame {
				t.Fatalf("expected game item, got %s", item.Type)
			}
		}
	}
}

func TestSearch(t *testing.T) {
	svc := NewService()
	results := svc.Search(SearchParams{Query: "zero", MediaType: "all"})

	if len(results) == 0 {
		t.Fatalf("expected search results")
	}

	if results[0].Title != "Zero Meridian" {
		t.Fatalf("expected top result Zero Meridian, got %s", results[0].Title)
	}
}

func TestSearchGames(t *testing.T) {
	svc := NewService()
	results := svc.Search(SearchParams{Query: "ghostline", MediaType: "game"})

	if len(results) == 0 {
		t.Fatalf("expected game search results")
	}

	if results[0].MediaType != string(MediaTypeGame) {
		t.Fatalf("expected game result, got %s", results[0].MediaType)
	}
}

func TestUpdateProgressInMemory(t *testing.T) {
	svc := NewService()
	userID := uuid.New()

	updated, err := svc.UpdateProgress(userID, ProgressUpdateInput{
		MediaID:         2,
		SeasonNumber:    1,
		EpisodeNumber:   7,
		ProgressPercent: 100,
	})
	if err != nil {
		t.Fatalf("expected update to succeed, got error: %v", err)
	}

	for _, entry := range updated {
		if entry.Item.ID == 2 &&
			entry.Progress.SeasonNumber == 1 &&
			entry.Progress.EpisodeNumber == 7 {
			t.Fatalf("expected completed progress to be excluded from continue watching")
		}
	}
}

func TestDashboardIncludesGameBacklog(t *testing.T) {
	svc := NewService()

	payload := svc.Dashboard()
	if len(payload.GameBacklog) == 0 {
		t.Fatalf("expected dashboard game backlog")
	}

	for _, item := range payload.GameBacklog {
		if item.Type != MediaTypeGame {
			t.Fatalf("expected game backlog item, got %s", item.Type)
		}
	}
}
