package deployer

import (
	"context"

	networkingv1 "k8s.io/api/networking/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

// IngressClient abstracts Kubernetes Ingress operations for testability.
type IngressClient interface {
	GetIngress(ctx context.Context, namespace, name string) (*networkingv1.Ingress, error)
	CreateIngress(ctx context.Context, namespace string, ingress *networkingv1.Ingress) (*networkingv1.Ingress, error)
	UpdateIngress(ctx context.Context, namespace string, ingress *networkingv1.Ingress) (*networkingv1.Ingress, error)
	DeleteIngress(ctx context.Context, namespace, name string) error
}

// k8sIngressClient wraps kubernetes.Interface to implement IngressClient.
type k8sIngressClient struct {
	clientset kubernetes.Interface
}

// GetIngress retrieves an Ingress by namespace and name.
func (c *k8sIngressClient) GetIngress(ctx context.Context, namespace, name string) (*networkingv1.Ingress, error) {
	return c.clientset.NetworkingV1().Ingresses(namespace).Get(ctx, name, metav1.GetOptions{})
}

// CreateIngress creates a new Ingress in the specified namespace.
func (c *k8sIngressClient) CreateIngress(ctx context.Context, namespace string, ingress *networkingv1.Ingress) (*networkingv1.Ingress, error) {
	return c.clientset.NetworkingV1().Ingresses(namespace).Create(ctx, ingress, metav1.CreateOptions{})
}

// UpdateIngress updates an existing Ingress in the specified namespace.
func (c *k8sIngressClient) UpdateIngress(ctx context.Context, namespace string, ingress *networkingv1.Ingress) (*networkingv1.Ingress, error) {
	return c.clientset.NetworkingV1().Ingresses(namespace).Update(ctx, ingress, metav1.UpdateOptions{})
}

// DeleteIngress deletes an Ingress by namespace and name.
func (c *k8sIngressClient) DeleteIngress(ctx context.Context, namespace, name string) error {
	return c.clientset.NetworkingV1().Ingresses(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
