// Copyright F3 Authors
// SPDX-License-Identifier: MIT

package forgejo

import (
	"context"
	"fmt"

	f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
	"code.forgejo.org/f3/gof3/v3/tree/generic"

	forgejo_sdk "code.forgejo.org/f3/gof3/v3/forges/forgejo/sdk"
)

type labels struct {
	container
}

func (o *labels) ListPage(ctx context.Context, node generic.NodeInterface, _ generic.ListOptions, page int) generic.ChildrenList {
	pageSize := o.getPageSize()

	var err error
	var forgejoLabels []*forgejo_sdk.Label

	owner := f3_tree.GetOwnerName(node)
	project := f3_tree.GetProjectName(node)

	forgejoLabels, _, err = o.getClient().ListRepoLabels(owner, project, forgejo_sdk.ListLabelsOptions{
		ListOptions: forgejo_sdk.ListOptions{Page: page, PageSize: pageSize},
	})
	if err != nil {
		panic(fmt.Errorf("error while listing labels %s/%s : %v", owner, project, err))
	}

	return f3_tree.ConvertListed(ctx, node, f3_tree.ConvertToAny(forgejoLabels...)...)
}

func newLabels() generic.NodeDriverInterface {
	return &labels{}
}
