/*
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package types

// OpenCollectiveRequest asks a node to initialize a collective session.
type OpenCollectiveRequest struct {
	TaskID         string         `json:"taskID"`
	SessionID      string         `json:"sessionID,omitempty"`
	ArtifactKey    string         `json:"artifactKey"`
	CollectiveSpec CollectiveSpec `json:"collectiveSpec"`
	EnableChunkCRC bool           `json:"enableChunkCRC"`
	TimeoutSeconds int32          `json:"timeoutSeconds,omitempty"`
}

// OpenCollectiveResponse returns the session metadata for a collective open.
type OpenCollectiveResponse struct {
	TaskID         string        `json:"taskID"`
	TransportPath  TransportPath `json:"transportPath"`
	ListenEndpoint string        `json:"listenEndpoint,omitempty"`
	Message        string        `json:"message,omitempty"`
}

// CollectiveStepRequest advances or acknowledges one collective iteration.
type CollectiveStepRequest struct {
	TaskID          string `json:"taskID"`
	SessionID       string `json:"sessionID,omitempty"`
	Iteration       int32  `json:"iteration"`
	ChunkCount      int32  `json:"chunkCount,omitempty"`
	BarrierOnly     bool   `json:"barrierOnly,omitempty"`
	AcknowledgeOnly bool   `json:"acknowledgeOnly,omitempty"`
}

// CollectiveStepResponse reports the state after a collective step request.
type CollectiveStepResponse struct {
	TaskID                string        `json:"taskID"`
	Iteration             int32         `json:"iteration"`
	Completed             bool          `json:"completed"`
	TransportPath         TransportPath `json:"transportPath,omitempty"`
	BytesTransferred      int64         `json:"bytesTransferred,omitempty"`
	AcknowledgedIteration int32         `json:"acknowledgedIteration,omitempty"`
	Message               string        `json:"message,omitempty"`
}

// CompleteCollectiveRequest asks a node to finalize a collective session.
type CompleteCollectiveRequest struct {
	TaskID      string `json:"taskID"`
	SessionID   string `json:"sessionID,omitempty"`
	Success     bool   `json:"success"`
	Message     string `json:"message,omitempty"`
	FinalCRC    bool   `json:"finalCRC,omitempty"`
	StagingPath string `json:"stagingPath,omitempty"`
}

// PushCollectiveChunkRequest sends one collective chunk to a peer.
type PushCollectiveChunkRequest struct {
	TaskID         string           `json:"taskID"`
	SessionID      string           `json:"sessionID,omitempty"`
	Iteration      int32            `json:"iteration"`
	ExpectedChunks int32            `json:"expectedChunks,omitempty"`
	Chunk          TransferredChunk `json:"chunk"`
	Data           []byte           `json:"data"`
	RelayOffset    int64            `json:"relayOffset,omitempty"`
	RelayRDMA      *RelayRDMAHint   `json:"relayRDMA,omitempty"`
	TransportPath  TransportPath    `json:"transportPath,omitempty"`
	SourceNode     string           `json:"sourceNode,omitempty"`
}

// PushCollectiveChunkResponse acknowledges one pushed collective chunk.
type PushCollectiveChunkResponse struct {
	TaskID        string        `json:"taskID"`
	Iteration     int32         `json:"iteration"`
	Accepted      bool          `json:"accepted"`
	TransportPath TransportPath `json:"transportPath,omitempty"`
	Message       string        `json:"message,omitempty"`
}

// CollectiveChunkPayload carries one chunk and its optional relay metadata.
type CollectiveChunkPayload struct {
	Chunk       TransferredChunk `json:"chunk"`
	Data        []byte           `json:"data"`
	RelayOffset int64            `json:"relayOffset,omitempty"`
	RelayRDMA   *RelayRDMAHint   `json:"relayRDMA,omitempty"`
	PublishSeq  int64            `json:"publishSeq,omitempty"`
}

// PushCollectiveChunksRequest sends multiple collective chunks in one request.
type PushCollectiveChunksRequest struct {
	TaskID         string                   `json:"taskID"`
	SessionID      string                   `json:"sessionID,omitempty"`
	Iteration      int32                    `json:"iteration"`
	ExpectedChunks int32                    `json:"expectedChunks,omitempty"`
	Chunks         []CollectiveChunkPayload `json:"chunks"`
	TransportPath  TransportPath            `json:"transportPath,omitempty"`
	SourceNode     string                   `json:"sourceNode,omitempty"`
}

// PushCollectiveChunksResponse acknowledges a batched chunk-push request.
type PushCollectiveChunksResponse struct {
	TaskID          string        `json:"taskID"`
	Iteration       int32         `json:"iteration"`
	Accepted        bool          `json:"accepted"`
	PublishedChunks int32         `json:"publishedChunks,omitempty"`
	FirstPublishSeq int64         `json:"firstPublishSeq,omitempty"`
	LastPublishSeq  int64         `json:"lastPublishSeq,omitempty"`
	TransportPath   TransportPath `json:"transportPath,omitempty"`
	Message         string        `json:"message,omitempty"`
}

// ListCollectiveChunksRequest asks a peer for chunks from one iteration.
type ListCollectiveChunksRequest struct {
	TaskID       string `json:"taskID"`
	SessionID    string `json:"sessionID,omitempty"`
	Iteration    int32  `json:"iteration"`
	RelativePath string `json:"relativePath,omitempty"`
}

// ListCollectiveChunksResponse returns the chunks available for one iteration.
type ListCollectiveChunksResponse struct {
	TaskID                string                   `json:"taskID"`
	Iteration             int32                    `json:"iteration"`
	ExpectedChunks        int32                    `json:"expectedChunks,omitempty"`
	AcknowledgedIteration int32                    `json:"acknowledgedIteration,omitempty"`
	Chunks                []CollectiveChunkPayload `json:"chunks"`
}

// ListCollectiveRelayChunksRequest asks for relay-published collective chunks.
type ListCollectiveRelayChunksRequest struct {
	TaskID                     string `json:"taskID"`
	SessionID                  string `json:"sessionID,omitempty"`
	AfterPublishSequence       int64  `json:"afterPublishSequence,omitempty"`
	AcknowledgePublishSequence int64  `json:"acknowledgePublishSequence,omitempty"`
	MaxChunks                  int32  `json:"maxChunks,omitempty"`
	MaxBytes                   int64  `json:"maxBytes,omitempty"`
	RelativePath               string `json:"relativePath,omitempty"`
}

// ListCollectiveRelayChunksResponse returns relay-published chunk payloads.
type ListCollectiveRelayChunksResponse struct {
	TaskID                      string                   `json:"taskID"`
	AfterPublishSequence        int64                    `json:"afterPublishSequence,omitempty"`
	PublishedSequence           int64                    `json:"publishedSequence,omitempty"`
	AcknowledgedPublishSequence int64                    `json:"acknowledgePublishSequence,omitempty"`
	TransportPath               TransportPath            `json:"transportPath,omitempty"`
	Completed                   bool                     `json:"completed,omitempty"`
	Chunks                      []CollectiveChunkPayload `json:"chunks"`
	Message                     string                   `json:"message,omitempty"`
}