package control

import (
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/common"
)

const serviceName = "control.ControlService"

const (
	rpcHealthCheck                       = "HealthCheck"
	rpcSetNetmapStatus                   = "SetNetmapStatus"
	rpcGetNetmapStatus                   = "GetNetmapStatus"
	rpcDropObjects                       = "DropObjects"
	rpcListShards                        = "ListShards"
	rpcSetShardMode                      = "SetShardMode"
	rpcSynchronizeTree                   = "SynchronizeTree"
	rpcEvacuateShard                     = "EvacuateShard"
	rpcStartShardEvacuation              = "StartShardEvacuation"
	rpcGetShardEvacuationStatus          = "GetShardEvacuationStatus"
	rpcResetShardEvacuationStatus        = "ResetShardEvacuationStatus"
	rpcStopShardEvacuation               = "StopShardEvacuation"
	rpcFlushCache                        = "FlushCache"
	rpcDoctor                            = "Doctor"
	rpcAddChainLocalOverride             = "AddChainLocalOverride"
	rpcGetChainLocalOverride             = "GetChainLocalOverride"
	rpcListChainLocalOverrides           = "ListChainLocalOverrides"
	rpcRemoveChainLocalOverride          = "RemoveChainLocalOverride"
	rpcRemoveChainLocalOverridesByTarget = "RemoveChainLocalOverridesByTarget"
	rpcSealWriteCache                    = "SealWriteCache"
	rpcListTargetsLocalOverrides         = "ListTargetsLocalOverrides"
	rpcDetachShards                      = "DetachShards"
	rpcStartShardRebuild                 = "StartShardRebuild"
)

// HealthCheck executes ControlService.HealthCheck RPC.
func HealthCheck(
	cli *client.Client,
	req *HealthCheckRequest,
	opts ...client.CallOption,
) (*HealthCheckResponse, error) {
	wResp := newResponseWrapper[HealthCheckResponse]()
	wReq := &requestWrapper{
		m: req,
	}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcHealthCheck), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// SetNetmapStatus executes ControlService.SetNetmapStatus RPC.
func SetNetmapStatus(
	cli *client.Client,
	req *SetNetmapStatusRequest,
	opts ...client.CallOption,
) (*SetNetmapStatusResponse, error) {
	wResp := newResponseWrapper[SetNetmapStatusResponse]()

	wReq := &requestWrapper{
		m: req,
	}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcSetNetmapStatus), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// GetNetmapStatus executes ControlService.GetNetmapStatus RPC.
func GetNetmapStatus(
	cli *client.Client,
	req *GetNetmapStatusRequest,
	opts ...client.CallOption,
) (*GetNetmapStatusResponse, error) {
	wResp := newResponseWrapper[GetNetmapStatusResponse]()

	wReq := &requestWrapper{
		m: req,
	}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcGetNetmapStatus), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// DropObjects executes ControlService.DropObjects RPC.
func DropObjects(
	cli *client.Client,
	req *DropObjectsRequest,
	opts ...client.CallOption,
) (*DropObjectsResponse, error) {
	wResp := newResponseWrapper[DropObjectsResponse]()

	wReq := &requestWrapper{
		m: req,
	}
	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcDropObjects), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// ListShards executes ControlService.ListShards RPC.
func ListShards(
	cli *client.Client,
	req *ListShardsRequest,
	opts ...client.CallOption,
) (*ListShardsResponse, error) {
	wResp := newResponseWrapper[ListShardsResponse]()

	wReq := &requestWrapper{
		m: req,
	}
	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcListShards), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// SetShardMode executes ControlService.SetShardMode RPC.
func SetShardMode(
	cli *client.Client,
	req *SetShardModeRequest,
	opts ...client.CallOption,
) (*SetShardModeResponse, error) {
	wResp := newResponseWrapper[SetShardModeResponse]()

	wReq := &requestWrapper{
		m: req,
	}
	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcSetShardMode), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// SynchronizeTree executes ControlService.SynchronizeTree RPC.
func SynchronizeTree(cli *client.Client, req *SynchronizeTreeRequest, opts ...client.CallOption) (*SynchronizeTreeResponse, error) {
	wResp := newResponseWrapper[SynchronizeTreeResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcSynchronizeTree), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// EvacuateShard executes ControlService.EvacuateShard RPC.
func EvacuateShard(cli *client.Client, req *EvacuateShardRequest, opts ...client.CallOption) (*EvacuateShardResponse, error) {
	wResp := newResponseWrapper[EvacuateShardResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcEvacuateShard), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// StartShardEvacuation executes ControlService.StartShardEvacuation RPC.
func StartShardEvacuation(cli *client.Client, req *StartShardEvacuationRequest, opts ...client.CallOption) (*StartShardEvacuationResponse, error) {
	wResp := newResponseWrapper[StartShardEvacuationResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcStartShardEvacuation), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// GetShardEvacuationStatus executes ControlService.GetShardEvacuationStatus RPC.
func GetShardEvacuationStatus(cli *client.Client, req *GetShardEvacuationStatusRequest, opts ...client.CallOption) (*GetShardEvacuationStatusResponse, error) {
	wResp := newResponseWrapper[GetShardEvacuationStatusResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcGetShardEvacuationStatus), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// StopShardEvacuation executes ControlService.StopShardEvacuation RPC.
func StopShardEvacuation(cli *client.Client, req *StopShardEvacuationRequest, opts ...client.CallOption) (*StopShardEvacuationResponse, error) {
	wResp := newResponseWrapper[StopShardEvacuationResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcStopShardEvacuation), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// ResetShardEvacuationStatus executes ControlService.ResetShardEvacuationStatus RPC.
func ResetShardEvacuationStatus(cli *client.Client, req *ResetShardEvacuationStatusRequest, opts ...client.CallOption) (*ResetShardEvacuationStatusResponse, error) {
	wResp := newResponseWrapper[ResetShardEvacuationStatusResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcResetShardEvacuationStatus), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// FlushCache executes ControlService.FlushCache RPC.
func FlushCache(cli *client.Client, req *FlushCacheRequest, opts ...client.CallOption) (*FlushCacheResponse, error) {
	wResp := newResponseWrapper[FlushCacheResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcFlushCache), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// Doctor executes ControlService.Doctor RPC.
func Doctor(cli *client.Client, req *DoctorRequest, opts ...client.CallOption) (*DoctorResponse, error) {
	wResp := newResponseWrapper[DoctorResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcDoctor), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// AddChainLocalOverride executes ControlService.AddChainLocalOverride RPC.
func AddChainLocalOverride(cli *client.Client, req *AddChainLocalOverrideRequest, opts ...client.CallOption) (*AddChainLocalOverrideResponse, error) {
	wResp := newResponseWrapper[AddChainLocalOverrideResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcAddChainLocalOverride), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// ListChainLocalOverrides executes ControlService.ListChainLocalOverrides RPC.
func ListChainLocalOverrides(cli *client.Client, req *ListChainLocalOverridesRequest, opts ...client.CallOption) (*ListChainLocalOverridesResponse, error) {
	wResp := newResponseWrapper[ListChainLocalOverridesResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcListChainLocalOverrides), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// ListTargetsLocalOverrides executes ControlService.ListTargetsLocalOverrides RPC.
func ListTargetsLocalOverrides(cli *client.Client, req *ListTargetsLocalOverridesRequest, opts ...client.CallOption) (*ListTargetsLocalOverridesResponse, error) {
	wResp := newResponseWrapper[ListTargetsLocalOverridesResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcListTargetsLocalOverrides), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// RemoveChainLocalOverride executes ControlService.RemoveChainLocalOverride RPC.
func GetChainLocalOverride(cli *client.Client, req *GetChainLocalOverrideRequest, opts ...client.CallOption) (*GetChainLocalOverrideResponse, error) {
	wResp := newResponseWrapper[GetChainLocalOverrideResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcGetChainLocalOverride), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// RemoveChainLocalOverride executes ControlService.RemoveChainLocalOverride RPC.
func RemoveChainLocalOverride(cli *client.Client, req *RemoveChainLocalOverrideRequest, opts ...client.CallOption) (*RemoveChainLocalOverrideResponse, error) {
	wResp := newResponseWrapper[RemoveChainLocalOverrideResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcRemoveChainLocalOverride), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// RemoveChainLocalOverridesByTarget executes ControlService.RemoveChainLocalOverridesByTarget RPC.
func RemoveChainLocalOverridesByTarget(cli *client.Client, req *RemoveChainLocalOverridesByTargetRequest, opts ...client.CallOption) (*RemoveChainLocalOverridesByTargetResponse, error) {
	wResp := newResponseWrapper[RemoveChainLocalOverridesByTargetResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcRemoveChainLocalOverridesByTarget), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// SealWriteCache executes ControlService.SealWriteCache RPC.
func SealWriteCache(cli *client.Client, req *SealWriteCacheRequest, opts ...client.CallOption) (*SealWriteCacheResponse, error) {
	wResp := newResponseWrapper[SealWriteCacheResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcSealWriteCache), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// DetachShards executes ControlService.DetachShards RPC.
func DetachShards(
	cli *client.Client,
	req *DetachShardsRequest,
	opts ...client.CallOption,
) (*DetachShardsResponse, error) {
	wResp := newResponseWrapper[DetachShardsResponse]()

	wReq := &requestWrapper{
		m: req,
	}
	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcDetachShards), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}

// StartShardRebuild executes ControlService.StartShardRebuild RPC.
func StartShardRebuild(cli *client.Client, req *StartShardRebuildRequest, opts ...client.CallOption) (*StartShardRebuildResponse, error) {
	wResp := newResponseWrapper[StartShardRebuildResponse]()
	wReq := &requestWrapper{m: req}

	err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcStartShardRebuild), wReq, wResp, opts...)
	if err != nil {
		return nil, err
	}

	return wResp.message, nil
}