package universities

import (
	"context"
	"fmt"
	"strings"

	"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
	"git.bissendorf.co/bissendorf/unifood/m/v2/model/external/stwbremen"
	"git.bissendorf.co/bissendorf/unifood/m/v2/util"
)

func getRestaurantList(ctx context.Context, client interfaces.QueryClient[stwbremen.RestaurantList]) ([]string, error) {
	list, err := client.Get(ctx,
		`page('essen-und-trinken')`,
		`{"items": "page.children.children"}`,
		false,
	)
	if err != nil {
		return nil, fmt.Errorf("failed to load university list: %w", err)
	}

	return util.Transform(list.Items, func(i *string) string {
		parts := strings.SplitN(*i, "/", 2)
		if len(parts) < 2 {
			return parts[0]
		}

		return parts[1]
	}), nil
}
