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

package forgejo

import (
	"context"

	"code.forgejo.org/f3/gof3/v3/id"
	"code.forgejo.org/f3/gof3/v3/kind"
	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"
)

func maybeMilestoneID(milestone *forgejo_sdk.Milestone) *int64 {
	if milestone != nil {
		id := milestone.ID
		return &id
	}
	return nil
}

func labelListToIDs(labels []*forgejo_sdk.Label) []int64 {
	ids := make([]int64, 0, len(labels))
	for _, label := range labels {
		ids = append(ids, label.ID)
	}
	return ids
}

type common struct {
	generic.NullDriver
}

func (o *common) GetHelper() any {
	panic("not implemented")
}

func (o *common) ListPage(ctx context.Context, node generic.NodeInterface, _ generic.ListOptions, page int) generic.ChildrenList {
	return generic.NewChildrenList(0)
}

func (o *common) getTree() generic.TreeInterface {
	return o.GetNode().GetTree()
}

func (o *common) getForge() *forge {
	return o.getTree().GetRoot().GetChild(id.NewNodeID(kind.KindForge)).GetDriver().(*forge)
}

func (o *common) getPageSize() int {
	return o.getTreeDriver().GetPageSize()
}

func (o *common) getF3Tree() f3_tree.TreeInterface {
	return o.getTree().(f3_tree.TreeInterface)
}

func (o *common) getKind() kind.Kind {
	return o.GetNode().GetKind()
}

func (o *common) getChildDriver(kind kind.Kind) generic.NodeDriverInterface {
	return o.GetNode().GetChild(id.NewNodeID(kind)).GetDriver()
}

func (o *common) getProject() *project {
	return f3_tree.GetProject(o.GetNode()).GetDriver().(*project)
}

func (o *common) getPullRequest() *pullRequest {
	return f3_tree.GetPullRequest(o.GetNode()).GetDriver().(*pullRequest)
}

func (o *common) isContainer() bool {
	return o.getF3Tree().IsContainer(o.getKind())
}

func (o *common) getPushURL() string {
	return o.getTreeDriver().options.GetPushURL()
}

func (o *common) getTreeDriver() *treeDriver {
	return o.GetTreeDriver().(*treeDriver)
}

func (o *common) getIsAdmin() bool {
	return o.getTreeDriver().GetIsAdmin()
}

func (o *common) getClient() *forgejo_sdk.Client {
	return o.getTreeDriver().GetClient()
}

func (o *common) isSelf(id int64) bool {
	return o.getTreeDriver().user.ID == id
}

func (o *common) maybeSudoName(name string) {
	o.getTreeDriver().MaybeSudoName(name)
}

func (o *common) maybeSudoID(ctx context.Context, id int64) {
	o.getTreeDriver().maybeSudoID(ctx, id)
}

func (o *common) notSudo() {
	o.getTreeDriver().NotSudo()
}

func (o *common) IsNull() bool { return false }
