package main

import (
	"fmt"

	"github.com/charmbracelet/log"
	"github.com/nicklaw5/helix/v2"
)

func categoryHelp() {
	fmt.Print("\nTwitch category commands.  Valid commands are:\n\n")
	fmt.Println("    search <search-term>: search twitch for categories matching search term.")
}

func categories(client *helix.Client, args []string) {
	if len(args) < 1 {
		log.Error("Insufficient arguments.")
		categoryHelp()
		return
	}

	switch args[0] {
	case "search":
		{
			data, err := searchCategories(client, args[1])
			if err != nil {
				log.Fatalf("Error searching categories: %v", err)
			}
			_ = data
		}
	default:
		categoryHelp()
	}
}

// searchCategories prints a list of categories matching searchTerm.  On success it also
// returns a list of categories.
func searchCategories(client *helix.Client, searchTerm string) ([]helix.Category, error) {
	resp, err := client.SearchCategories(&helix.SearchCategoriesParams{
		Query: searchTerm,
	})
	if err != nil {
		log.Fatalf("Error while searching for category '%v'\n", searchTerm)
		return nil, err
	}

	// TODO: Pagination and such
	for _, category := range resp.Data.Categories {
		fmt.Printf("[%v] %v\n", category.ID, category.Name)
	}

	return resp.Data.Categories, nil
}
