package k8s

import (
	"context"

	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

type Client struct {
	clientset *kubernetes.Clientset
}

func NewClient() (*Client, error) {
	config, err := rest.InClusterConfig()
	if err != nil {
		// Fall back to out-of-cluster config for local development
		// config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", "config"))
		// if err != nil {
		// 	return nil, err
		// }
	}
	
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}
	
	return &Client{
		clientset: clientset,
	}, nil
}

func (c *Client) Clientset() *kubernetes.Clientset {
	return c.clientset
}

func (c *Client) GetPods(ctx context.Context, namespace string) ([]v1.Pod, error) {
	pods, err := c.clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
	if err != nil {
		return nil, err
	}
	return pods.Items, nil
}

func (c *Client) WatchPods(ctx context.Context, namespace string, resourceVersion string) (watch.Interface, error) {
	return c.clientset.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{
		ResourceVersion: resourceVersion,
	})
}
