package utilities

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/disks"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/disks/models"
)

func UtilityDiskReplicationUpdateStartStop(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	diskId := uint64(state.DiskId.ValueInt64())
	targetDiskId := uint64(state.ReplicationId.ValueInt64())

	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdateStartStop: start update for disk replica with ID: %d", diskId))

	ok := !(state.Start.IsNull() || state.Start.IsUnknown())
	start := state.Start.ValueBool()

	if ok && start {
		tflog.Info(ctx, fmt.Sprintf("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.CloudBroker().Disks().ReplicationStart(ctx, req)
		if err != nil {
			diags.AddError("UtilityDiskReplicationUpdateStartStop: Unable to start replicate disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdateStartStop: start disk replication from Disk with ID: %d to Disk with ID: %d, complete", diskId, targetDiskId))

	}

	if ok && !start {
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdateStartStop: stop disk replication from Disk with ID: %d to Disk with ID: %d", targetDiskId, diskId))
		req := disks.ReplicationStopRequest{
			DiskID: targetDiskId,
		}
		_, err := c.CloudBroker().Disks().ReplicationStop(ctx, req)
		if err != nil {
			diags.AddError("UtilityDiskReplicationUpdateStartStop: Unable to stop replicate disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdateStartStop: stop disk replication from Disk with ID: %d to Disk with ID: %d, complete", targetDiskId, diskId))
	}
	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdateStartStop: complete update for disk replica with ID: %d", diskId))
	return nil
}

func UtilityDiskReplicationUpdatePause(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	diskId := uint64(state.DiskId.ValueInt64())
	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: start update for disk replica with ID: %d", diskId))
	pause := state.Pause.ValueBool()
	ok := !(state.Pause.IsNull() || state.Pause.IsUnknown())

	if ok && pause {
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: pause disk replication with ID: %d", diskId))
		req := disks.ReplicationSuspendRequest{
			DiskID: diskId,
		}
		_, err := c.CloudBroker().Disks().ReplicationSuspend(ctx, req)
		if err != nil {
			diags.AddError("utilityDiskReplicationUpdatePause: Unable to pause disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: pause disk replication with ID: %d, complete", diskId))
	}

	if ok && !pause {
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: resume disk replication with ID: %d", diskId))
		req := disks.ReplicationResumeRequest{
			DiskID: diskId,
		}
		_, err := c.CloudBroker().Disks().ReplicationResume(ctx, req)
		if err != nil {
			diags.AddError("utilityDiskReplicationUpdatePause: Unable to resume disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: resume disk replication with ID: %d, complete", diskId))
	}

	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicationUpdatePause: complete update for disk replica with ID: %d", diskId))
	return nil
}

func UtilityDiskReplicationUpdateReverse(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}
	diskId := uint64(state.DiskId.ValueInt64())
	targetDiskId := uint64(state.ReplicationId.ValueInt64())

	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: start update for disk replica with ID: %d", diskId))

	reverse := state.Reverse.ValueBool()
	ok := !(state.Reverse.IsNull() || state.Reverse.IsUnknown())

	if ok && reverse {
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d", diskId, targetDiskId))
		req := disks.ReplicationReverseRequest{
			DiskID: diskId,
		}
		_, err := c.CloudBroker().Disks().ReplicationReverse(ctx, req)
		if err != nil {
			diags.AddError("utilityDiskReplicationUpdateReverse: Unable to reverse disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d, complete", diskId, targetDiskId))
	}

	if ok && !reverse {
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d", targetDiskId, diskId))
		req := disks.ReplicationReverseRequest{
			DiskID: targetDiskId,
		}
		_, err := c.CloudBroker().Disks().ReplicationReverse(ctx, req)
		if err != nil {
			diags.AddError("utilityDiskReplicationUpdateReverse: Unable to reverse disk", err.Error())
			return diags
		}
		tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: reverse disk replication from Disk with ID: %d to Disk with ID: %d, complete", targetDiskId, diskId))
	}

	tflog.Info(ctx, fmt.Sprintf("utilityDiskReplicaUpdateReverse: complete update for disk replica with ID: %d", diskId))
	return nil
}

func ResourceDiskReplicationCheckPresence(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *client.Client) (*disks.RecordDisk, *string, error) {

	status, err := c.CloudBroker().Disks().ReplicationStatus(ctx, disks.ReplicationStatusRequest{DiskID: uint64(state.DiskId.ValueInt64())})
	if err != nil {
		return nil, nil, err
	}

	req := disks.GetRequest{}

	if !state.DiskId.IsNull() && !state.DiskId.IsUnknown() {
		req.DiskID = uint64(state.DiskId.ValueInt64())
	} else {
		req.DiskID = uint64(state.ReplicationId.ValueInt64())
	}

	tflog.Info(ctx, "ResourceDiskReplicationCheckPresence: load disk")
	disk, err := c.CloudBroker().Disks().Get(ctx, req)
	if err != nil {
		return nil, nil, err
	}

	return disk, &status, nil
}

// DiskReadStatus loads disk resource by ids id, gets it current status.
// In case of failure returns errors.
func ReplicationDiskReadStatus(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "ReplicationDiskReadStatus: Read status disk with ID", map[string]any{"disk_id": state.DiskId.ValueInt64()})

	diags := diag.Diagnostics{}

	_, _, err := ResourceDiskReplicationCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("ReplicationDiskReadStatus: Unable to Read Disk before status check", err.Error())
		return diags
	}

	return nil
}
