/*
Copyright (c) 2019-2024 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>

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.
*/

/*
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
Orchestration Technology) with Terraform by Hashicorp.

Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort

Please see README.md to learn where to place source code so that it
builds seamlessly.

Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
*/

package disks

import (
	"context"
	"strconv"

	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/disks"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func utilityDiskReplicationUpdateStartStop(ctx context.Context, d *schema.ResourceData, m interface{}) error {
	diskId := uint64(d.Get("disk_id").(int))
	targetDiskId, _ := strconv.ParseUint(d.Id(), 10, 64)

	log.Debugf("utilityDiskReplicationUpdateStartStop: start update for disk replica with ID: %d", diskId)
	c := m.(*controller.ControllerCfg)

	start, ok := d.GetOk("start")

	if ok && start.(bool) {
		log.Debugf("utilityDiskReplicationUpdateStartStop: start disk replication from Disk with ID: %d to Disk with ID: %d", diskId, targetDiskId)
		req := disks.ReplicationStartRequest{
			DiskID: diskId,
			TargetDiskID: targetDiskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationStart(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicationUpdateStartStop: start disk replication from Disk with ID: %d to Disk with ID: %d, complete", diskId, targetDiskId)
	}

	if ok && !start.(bool) {
		log.Debugf("utilityDiskReplicationUpdateStartStop: stop disk replication from Disk with ID: %d to Disk with ID: %d", targetDiskId, diskId)
		req := disks.ReplicationStopRequest{
			DiskID: targetDiskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationStop(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicationUpdateStartStop: stop disk replication from Disk with ID: %d to Disk with ID: %d, complete", targetDiskId, diskId)
	}

	log.Debugf("utilityDiskReplicationUpdateStartStop: complete update for disk replica with ID: %d", diskId)
	return nil
}

func utilityDiskReplicationUpdatePause(ctx context.Context, d *schema.ResourceData, m interface{}) error {
	diskId := uint64(d.Get("disk_id").(int))
	log.Debugf("utilityDiskReplicationUpdatePause: start update for disk replica with ID: %d", diskId)
	c := m.(*controller.ControllerCfg)

	pause, ok := d.GetOk("pause")

	if ok && pause.(bool) {
		log.Debugf("utilityDiskReplicationUpdatePause: pause disk replication with ID: %d", diskId)
		req := disks.ReplicationSuspendRequest{
			DiskID: diskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationSuspend(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicationUpdatePause: pause disk replication with ID: %d, complete", diskId)
	}

	if ok && !pause.(bool) {
		log.Debugf("utilityDiskReplicationUpdatePause: resume disk replication with ID: %d", diskId)
		req := disks.ReplicationResumeRequest{
			DiskID: diskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationResume(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicationUpdatePause: resume disk replication with ID: %d, complete", diskId)
	}

	log.Debugf("utilityDiskReplicationUpdatePause: complete update for disk replica with ID: %d", diskId)
	return nil
}

func utilityDiskReplicationUpdateReverse(ctx context.Context, d *schema.ResourceData, m interface{}) error {
	diskId := uint64(d.Get("disk_id").(int))
	targetDiskId, _ := strconv.ParseUint(d.Id(), 10, 64)

	log.Debugf("utilityDiskReplicaUpdateReverse: start update for disk replica with ID: %d", diskId)
	c := m.(*controller.ControllerCfg)

	reverse, ok := d.GetOk("reverse")

	if ok && reverse.(bool) {
		log.Debugf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d", diskId, targetDiskId)
		req := disks.ReplicationReverseRequest{
			DiskID: diskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationReverse(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d, complete", diskId, targetDiskId)
	}

	if ok && !reverse.(bool) {
		log.Debugf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d", targetDiskId, diskId)
		req := disks.ReplicationReverseRequest{
			DiskID: targetDiskId,
		}
		_, err := c.CloudAPI().Disks().ReplicationReverse(ctx, req)
		if err != nil {
			return err
		}
		log.Debugf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d, complete", targetDiskId, diskId)
	}

	log.Debugf("utilityDiskReplicaUpdateReverse: complete update for disk replica with ID: %d", diskId)
	return nil
}

func utilityDiskReplicaCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*disks.RecordDisk, error) {
	c := m.(*controller.ControllerCfg)

	req := disks.GetRequest{}

	if d.Id() != "" {
		diskId, _ := strconv.ParseUint(d.Id(), 10, 64)
		req.DiskID = diskId
	} else {
		req.DiskID = uint64(d.Get("replica_disk_id").(int))
	}

	log.Debugf("utilityDiskReplicaCheckPresence: load disk")
	disk, err := c.CloudAPI().Disks().Get(ctx, req)
	if err != nil {
		return nil, err
	}

	return disk, nil
}
