// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package alicloud

import (
	"context"
	"fmt"
	"time"

	corev1 "k8s.io/api/core/v1"
	storagev1 "k8s.io/api/storage/v1"

	"github.com/pingcap/tidb-operator/pkg/manager/volumes/delegation"
)

const DiskPluginProvisioner = "diskplugin.csi.alibabacloud.com"

var _ delegation.VolumeModifier = &DiskPluginModifier{}

type DiskPluginModifier struct{}

func NewDiskPluginModifier() delegation.VolumeModifier {
	return &DiskPluginModifier{}
}

func (m *DiskPluginModifier) Name() string {
	return DiskPluginProvisioner
}

func (m *DiskPluginModifier) MinWaitDuration() time.Duration {
	return time.Minute
}

func (m *DiskPluginModifier) ModifyVolume(ctx context.Context, pvc *corev1.PersistentVolumeClaim, pv *corev1.PersistentVolume, sc *storagev1.StorageClass) (bool, error) {
	return false, nil
}

func (m *DiskPluginModifier) Validate(spvc, dpvc *corev1.PersistentVolumeClaim, ssc, dsc *storagev1.StorageClass) error {
	if ssc.Provisioner != dsc.Provisioner {
		return fmt.Errorf("provisioner should not be changed, now from %s to %s", ssc.Provisioner, dsc.Provisioner)
	}
	if ssc.Name != dsc.Name {
		return fmt.Errorf("storage class should not be changed, now from %s to %s", ssc.Name, dsc.Name)
	}

	return nil
}
