package operator

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"

	v1alpha1 "github.com/pfeifferj/container-mom-go/pkg/apis/containermom/v1alpha1"
	"github.com/pfeifferj/container-mom-go/pkg/config"
)

func TestOperator(t *testing.T) {
	// Create test config
	cfg := &config.Config{
		ListenAddr: ":8081",
		MongoURI:   "mongodb://localhost:27017",
	}

	// Create operator
	op := New(cfg, nil)
	assert.NotNil(t, op)

	// Test deployment trigger
	err := op.TriggerDeploymentReconcile(context.Background(), "test-deployment")
	assert.Error(t, err) // Should fail because deployment doesn't exist

	// Test MongoDB connection
	client := op.GetMongoDB()
	assert.NotNil(t, client)
	err = client.Ping(context.Background(), nil)
	assert.NoError(t, err)
}

func TestOperatorHealth(t *testing.T) {
	// Create test config
	cfg := &config.Config{
		ListenAddr: ":8081",
		MongoURI:   "mongodb://localhost:27017",
	}

	// Create operator
	op := New(cfg, nil)
	assert.NotNil(t, op)

	// Test health check
	err := op.GetMongoDB().Ping(context.Background(), nil)
	assert.NoError(t, err)
}

func TestOperatorBilling(t *testing.T) {
	op := &Operator{}

	t.Run("GetCurrentCustomer", func(t *testing.T) {
		customer, err := op.GetCurrentCustomer(context.Background())
		assert.NoError(t, err)
		assert.Equal(t, "default", customer.Name)
	})

	t.Run("GetResourceUsage", func(t *testing.T) {
		usage, err := op.GetResourceUsage(context.Background(), "test")
		assert.NoError(t, err)
		assert.NotNil(t, usage)
	})

	t.Run("GetBillingHistory", func(t *testing.T) {
		history, err := op.GetBillingHistory(context.Background(), "test")
		assert.NoError(t, err)
		assert.Empty(t, history)
	})

	t.Run("UpdateBillingPlan", func(t *testing.T) {
		err := op.UpdateBillingPlan(context.Background(), "test", "plan")
		assert.NoError(t, err)
	})

	t.Run("UpdateBillingStatus", func(t *testing.T) {
		err := op.UpdateBillingStatus(context.Background(), "test", v1alpha1.BillingStatus{})
		assert.NoError(t, err)
	})
}
