package v1_0

import (
	"context"

	"github.com/grafana/grafana-app-sdk/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CustomKindClient struct {
	client *resource.TypedClient[*CustomKind, *CustomKindList]
}

func NewCustomKindClient(client resource.Client) *CustomKindClient {
	return &CustomKindClient{
		client: resource.NewTypedClient[*CustomKind, *CustomKindList](client, CustomKindKind()),
	}
}

func NewCustomKindClientFromGenerator(generator resource.ClientGenerator) (*CustomKindClient, error) {
	c, err := generator.ClientFor(CustomKindKind())
	if err != nil {
		return nil, err
	}
	return NewCustomKindClient(c), nil
}

func (c *CustomKindClient) Get(ctx context.Context, identifier resource.Identifier) (*CustomKind, error) {
	return c.client.Get(ctx, identifier)
}

func (c *CustomKindClient) List(ctx context.Context, namespace string, opts resource.ListOptions) (*CustomKindList, error) {
	return c.client.List(ctx, namespace, opts)
}

func (c *CustomKindClient) ListAll(ctx context.Context, namespace string, opts resource.ListOptions) (*CustomKindList, error) {
	resp, err := c.client.List(ctx, namespace, resource.ListOptions{
		ResourceVersion: opts.ResourceVersion,
		Limit:           opts.Limit,
		LabelFilters:    opts.LabelFilters,
		FieldSelectors:  opts.FieldSelectors,
	})
	if err != nil {
		return nil, err
	}
	for resp.GetContinue() != "" {
		page, err := c.client.List(ctx, namespace, resource.ListOptions{
			Continue:        resp.GetContinue(),
			ResourceVersion: opts.ResourceVersion,
			Limit:           opts.Limit,
			LabelFilters:    opts.LabelFilters,
			FieldSelectors:  opts.FieldSelectors,
		})
		if err != nil {
			return nil, err
		}
		resp.SetContinue(page.GetContinue())
		resp.SetResourceVersion(page.GetResourceVersion())
		resp.SetItems(append(resp.GetItems(), page.GetItems()...))
	}
	return resp, nil
}

func (c *CustomKindClient) Create(ctx context.Context, obj *CustomKind, opts resource.CreateOptions) (*CustomKind, error) {
	// Make sure apiVersion and kind are set
	obj.APIVersion = GroupVersion.Identifier()
	obj.Kind = CustomKindKind().Kind()
	return c.client.Create(ctx, obj, opts)
}

func (c *CustomKindClient) Update(ctx context.Context, obj *CustomKind, opts resource.UpdateOptions) (*CustomKind, error) {
	return c.client.Update(ctx, obj, opts)
}

func (c *CustomKindClient) Patch(ctx context.Context, identifier resource.Identifier, req resource.PatchRequest, opts resource.PatchOptions) (*CustomKind, error) {
	return c.client.Patch(ctx, identifier, req, opts)
}

func (c *CustomKindClient) UpdateStatus(ctx context.Context, identifier resource.Identifier, newStatus CustomKindStatus, opts resource.UpdateOptions) (*CustomKind, error) {
	return c.client.Update(ctx, &CustomKind{
		TypeMeta: metav1.TypeMeta{
			Kind:       CustomKindKind().Kind(),
			APIVersion: GroupVersion.Identifier(),
		},
		ObjectMeta: metav1.ObjectMeta{
			ResourceVersion: opts.ResourceVersion,
			Namespace:       identifier.Namespace,
			Name:            identifier.Name,
		},
		Status: newStatus,
	}, resource.UpdateOptions{
		Subresource:     "status",
		ResourceVersion: opts.ResourceVersion,
	})
}

func (c *CustomKindClient) Delete(ctx context.Context, identifier resource.Identifier, opts resource.DeleteOptions) error {
	return c.client.Delete(ctx, identifier, opts)
}
