// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kube

import (
	"context"
	"fmt"

	core "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	meta "k8s.io/apimachinery/pkg/apis/meta/v1"
	kube "k8s.io/client-go/kubernetes"

	log "github.com/altinity/clickhouse-operator/pkg/announcer"
	api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
	"github.com/altinity/clickhouse-operator/pkg/controller"
	"github.com/altinity/clickhouse-operator/pkg/controller/common/poller"
	"github.com/altinity/clickhouse-operator/pkg/interfaces"
)

type Secret struct {
	kubeClient kube.Interface
	namer      interfaces.INameManager
}

func NewSecret(kubeClient kube.Interface, namer interfaces.INameManager) *Secret {
	return &Secret{
		kubeClient: kubeClient,
		namer:      namer,
	}
}

// Get gets Secret. Accepted types:
//  1. *core.Service
//  2. *chop.Host
func (c *Secret) Get(ctx context.Context, params ...any) (*core.Secret, error) {
	var name, namespace string
	switch len(params) {
	case 2:
		// Expecting namespace name
		namespace = params[0].(string)
		name = params[1].(string)
	case 1:
		// Expecting obj
		obj := params[0]
		switch typedObj := obj.(type) {
		case *core.Secret:
			name = typedObj.Name
			namespace = typedObj.Namespace
		case *api.Host:
			name = c.namer.Name(interfaces.NameStatefulSetService, typedObj)
			namespace = typedObj.Runtime.Address.Namespace
		}
	}
	ctx = k8sCtx(ctx)
	return c.kubeClient.CoreV1().Secrets(namespace).Get(ctx, name, controller.NewGetOptions())
}

func (c *Secret) Create(ctx context.Context, svc *core.Secret) (*core.Secret, error) {
	ctx = k8sCtx(ctx)
	return c.kubeClient.CoreV1().Secrets(svc.Namespace).Create(ctx, svc, controller.NewCreateOptions())
}

func (c *Secret) Update(ctx context.Context, svc *core.Secret) (*core.Secret, error) {
	ctx = k8sCtx(ctx)
	return c.kubeClient.CoreV1().Secrets(svc.Namespace).Update(ctx, svc, controller.NewUpdateOptions())
}

func (c *Secret) Remove(ctx context.Context, namespace, name string) error {
	ctx = k8sCtx(ctx)
	return c.kubeClient.CoreV1().Secrets(namespace).Delete(ctx, name, controller.NewDeleteOptions())
}

func (c *Secret) Delete(ctx context.Context, namespace, name string) error {
	item := "Secret"
	return poller.New(ctx, fmt.Sprintf("delete %s: %s/%s", item, namespace, name)).
		WithOptions(poller.NewOptionsFromConfig()).
		WithFunctions(&poller.Functions{
			IsDone: func(_ctx context.Context, _ any) bool {
				if err := c.Remove(ctx, namespace, name); err != nil {
					if !errors.IsNotFound(err) {
						log.V(1).Warning("Error deleting %s: %s/%s err: %v ", item, namespace, name, err)
					}
				}

				_, err := c.Get(ctx, namespace, name)
				return errors.IsNotFound(err)
			},
		}).Poll()
}

func (c *Secret) List(ctx context.Context, namespace string, opts meta.ListOptions) ([]core.Secret, error) {
	ctx = k8sCtx(ctx)
	list, err := c.kubeClient.CoreV1().Secrets(namespace).List(ctx, opts)
	if err != nil {
		return nil, err
	}
	if list == nil {
		return nil, err
	}
	return list.Items, nil
}
