// Copyright 2026 PingCAP, Inc.
//
// 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.

//go:build e2e

package e2e

import (
	"context"
	"time"

	"github.com/onsi/ginkgo/v2"

	"github.com/tidbcloud/cluster-service-ng/internal/capacity-planner/test/e2e/framework"
	"github.com/tidbcloud/cluster-service-ng/internal/capacity-planner/test/e2e/suite/tidb"
)

const environmentVerificationTimeout = 2 * time.Minute

var e2eFramework *framework.Framework

func init() {
	tidb.Register(func() *framework.Framework { return e2eFramework })
}

// Shared dependencies and capacity-planner are prepared by make test/e2e.
var _ = ginkgo.SynchronizedBeforeSuite(func(ctx context.Context) []byte {
	e2eFramework = framework.New()

	ginkgo.By("Verifying the shared admin namespace")
	e2eFramework.VerifyAdminNamespace(ctx)

	ginkgo.By("Verifying the Autoscaler CRD")
	e2eFramework.VerifyAutoscalerCRD(ctx)

	ginkgo.By("Verifying metrics-server")
	e2eFramework.VerifyMetricsServer(ctx)

	ginkgo.By("Verifying TiDB Operator")
	e2eFramework.VerifyTiDBOperator(ctx)

	ginkgo.By("Verifying the capacity planner Deployment")
	e2eFramework.VerifyCapacityPlanner(ctx)

	return nil
}, func(context.Context, []byte) {
	// Serial specs run on process 1. Other processes only need clients if the
	// suite is ever invoked with Ginkgo's parallel mode.
	if e2eFramework == nil {
		e2eFramework = framework.New()
	}
}, ginkgo.NodeTimeout(environmentVerificationTimeout))
