// Copyright 2026 Microsoft Corporation
//
// 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 datadump

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"

	azcorearm "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"

	"github.com/Azure/ARO-HCP/backend/pkg/utils/controllerutils"
	"github.com/Azure/ARO-HCP/internal/database/cosmosstorage/billingcosmosstorage"
	"github.com/Azure/ARO-HCP/internal/database/cosmosstoragetesting/billingcosmosstoragetesting"
	"github.com/Azure/ARO-HCP/internal/database/cosmosstoragetesting/corecosmosstoragetesting"
)

func TestBillingDumpController_SyncOnce(t *testing.T) {
	ctx := context.Background()

	clusterResourceID, err := azcorearm.ParseResourceID("/subscriptions/sub-1/resourceGroups/rg-1/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/cluster-1")
	require.NoError(t, err)

	mockResourcesDBClient := corecosmosstoragetesting.NewMockResourcesDBClient()

	mockBillingDBClient := billingcosmosstoragetesting.NewMockBillingDBClient()
	syncer := &billingDump{
		resourcesDBClient: mockResourcesDBClient,
		billingDBClient:   mockBillingDBClient,
		nextDumpChecker:   &alwaysSyncCooldownChecker{},
	}

	key := controllerutils.HCPClusterKey{
		SubscriptionID:    clusterResourceID.SubscriptionID,
		ResourceGroupName: clusterResourceID.ResourceGroupName,
		HCPClusterName:    clusterResourceID.Name,
	}

	// SyncOnce should never return an error (best effort)
	err = syncer.SyncOnce(ctx, key)
	require.NoError(t, err)
}

func TestNewBillingDumpController(t *testing.T) {
	// Test that constructor doesn't panic
	// Note: We can't easily test the wrapped controller without backendInformers,
	// so we just verify the syncer directly in other tests
	require.NotPanics(t, func() {
		// The constructor would require backendInformers which needs ResourcesGlobalListers
		// This is tested indirectly through other tests
	})
}

func TestBillingDumpController_SyncOnce_WithBillingDoc(t *testing.T) {
	ctx := context.Background()

	clusterResourceID, err := azcorearm.ParseResourceID("/subscriptions/sub-1/resourceGroups/rg-1/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/cluster-1")
	require.NoError(t, err)

	mockResourcesDBClient := corecosmosstoragetesting.NewMockResourcesDBClient()
	mockBillingDBClient := billingcosmosstoragetesting.NewMockBillingDBClient()

	// Create billing document
	billingDoc := billingcosmosstorage.NewBillingDocument("billing-doc-1", clusterResourceID)
	err = mockBillingDBClient.BillingDocs(clusterResourceID.SubscriptionID).Create(ctx, billingDoc)
	require.NoError(t, err)

	syncer := &billingDump{
		resourcesDBClient: mockResourcesDBClient,
		billingDBClient:   mockBillingDBClient,
		nextDumpChecker:   &alwaysSyncCooldownChecker{},
	}

	key := controllerutils.HCPClusterKey{
		SubscriptionID:    clusterResourceID.SubscriptionID,
		ResourceGroupName: clusterResourceID.ResourceGroupName,
		HCPClusterName:    clusterResourceID.Name,
	}

	// SyncOnce should never return an error (best effort)
	err = syncer.SyncOnce(ctx, key)
	require.NoError(t, err)
}

func TestBillingDumpController_CooldownRespected(t *testing.T) {
	ctx := context.Background()

	clusterResourceID, err := azcorearm.ParseResourceID("/subscriptions/sub-1/resourceGroups/rg-1/providers/Microsoft.RedHatOpenShift/hcpOpenShiftClusters/cluster-1")
	require.NoError(t, err)

	mockResourcesDBClient := corecosmosstoragetesting.NewMockResourcesDBClient()

	// neverSyncChecker prevents sync
	neverSyncChecker := cooldownCheckerFunc(func(ctx context.Context, key any) bool {
		return false
	})

	// Test that cooldown prevents sync
	mockBillingDBClient := billingcosmosstoragetesting.NewMockBillingDBClient()
	syncer := &billingDump{
		resourcesDBClient: mockResourcesDBClient,
		billingDBClient:   mockBillingDBClient,
		nextDumpChecker:   neverSyncChecker,
	}

	key := controllerutils.HCPClusterKey{
		SubscriptionID:    clusterResourceID.SubscriptionID,
		ResourceGroupName: clusterResourceID.ResourceGroupName,
		HCPClusterName:    clusterResourceID.Name,
	}

	// Should succeed but not actually dump (cooldown prevents it)
	err = syncer.SyncOnce(ctx, key)
	require.NoError(t, err)
}

// cooldownCheckerFunc is a function type that implements CooldownChecker
type cooldownCheckerFunc func(ctx context.Context, key any) bool

func (f cooldownCheckerFunc) CanSync(ctx context.Context, key any) bool {
	return f(ctx, key)
}
