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

// ByteRange 定义文件片段,End 为排他上界。
type ByteRange struct {
	RelativePath string `json:"relativePath"`
	Start        int64  `json:"start"`
	End          int64  `json:"end"`
	RelayOffset  int64  `json:"relayOffset,omitempty"`
}

// SourceEndpoint 描述一个可访问的数据源。
type SourceEndpoint struct {
	SourceID    string         `json:"sourceID"`
	SourceType  string         `json:"sourceType"`
	NodeName    string         `json:"nodeName,omitempty"`
	Endpoint    string         `json:"endpoint"`
	Path        string         `json:"path"`
	AccessToken string         `json:"accessToken,omitempty"`
	RelayRDMA   *RelayRDMAHint `json:"relayRDMA,omitempty"`
}

// SourceSegmentPlan 描述目标节点从某个源读取的分片计划。
type SourceSegmentPlan struct {
	SourceID       string         `json:"sourceID"`
	SourceEndpoint SourceEndpoint `json:"sourceEndpoint"`
	ByteRanges     []ByteRange    `json:"byteRanges,omitempty"`
	Weight         int32          `json:"weight,omitempty"`
	MaxParallelism int32          `json:"maxParallelism,omitempty"`
}

// TopologyHints 作为后续拓扑优化的稳定扩展字段。
type TopologyHints struct {
	NetworkDomain string `json:"networkDomain,omitempty"`
	SameRack      bool   `json:"sameRack,omitempty"`
	SuggestedQPS  int32  `json:"suggestedQps,omitempty"`
}

// TargetTransferPlan 描述单个目标节点的拉取计划。
type TargetTransferPlan struct {
	TargetNode      string              `json:"targetNode"`
	TransferMode    TransferMode        `json:"transferMode"`
	SourceSegments  []SourceSegmentPlan `json:"sourceSegments"`
	FallbackSources []SourceEndpoint    `json:"fallbackSources,omitempty"`
	TopologyHints   TopologyHints       `json:"topologyHints,omitempty"`
}

// WarmupDistributionPlan 表示批次级分发输出。
type WarmupDistributionPlan struct {
	PlanID      string               `json:"planID"`
	Strategy    DistributionStrategy `json:"strategy"`
	EstimatedMs int64                `json:"estimatedMs,omitempty"`
	TargetPlans []TargetTransferPlan `json:"targetPlans"`
}

// TransferSpec 是数据面的唯一输入对象。
type TransferSpec struct {
	TaskID            string              `json:"taskID"`
	ArtifactKey       string              `json:"artifactKey"`
	TransferMode      TransferMode        `json:"transferMode"`
	LogicalManifest   LogicalManifest     `json:"logicalManifest"`
	CollectiveSpec    CollectiveSpec      `json:"collectiveSpec,omitempty"`
	SourceSegments    []SourceSegmentPlan `json:"sourceSegments"`
	FallbackSources   []SourceEndpoint    `json:"fallbackSources,omitempty"`
	TargetTempPath    string              `json:"targetTempPath"`
	TargetFinalPath   string              `json:"targetFinalPath"`
	PreserveExisting  bool                `json:"preserveExisting,omitempty"`
	ForceSyncClose    bool                `json:"forceSyncClose,omitempty"`
	EnableChunkCRC32C bool                `json:"enableChunkCRC32C,omitempty"`
	ChunkSizeBytes    int64               `json:"chunkSizeBytes"`
	Parallelism       int32               `json:"parallelism,omitempty"`
	RetryLimit        int32               `json:"retryLimit,omitempty"`
	TimeoutSeconds    int32               `json:"timeoutSeconds,omitempty"`
}

// TransferredChunk 保存传输完成后的分块校验元数据。
type TransferredChunk struct {
	ChunkID      string `json:"chunkID"`
	FilePath     string `json:"filePath"`
	RelativePath string `json:"relativePath"`
	Offset       int64  `json:"offset"`
	Size         int64  `json:"size"`
	CRC32C       string `json:"crc32c,omitempty"`
	SourceID     string `json:"sourceID,omitempty"`
}

// TransferResult 是数据面执行结果。
type TransferResult struct {
	TaskID             string              `json:"taskID"`
	TempPath           string              `json:"tempPath"`
	BytesTransferred   int64               `json:"bytesTransferred"`
	ChunkCount         int32               `json:"chunkCount"`
	SucceededChunks    int32               `json:"succeededChunks"`
	FailedChunks       int32               `json:"failedChunks"`
	ThroughputMBps     float64             `json:"throughputMBps"`
	TransportPath      TransportPath       `json:"transportPath,omitempty"`
	SourceID           string              `json:"sourceID,omitempty"`
	StartedAt          int64               `json:"startedAt"`
	FinishedAt         int64               `json:"finishedAt"`
	TransferredChunks  []TransferredChunk  `json:"transferredChunks,omitempty"`
	SolidifiedManifest *SolidifiedManifest `json:"solidifiedManifest,omitempty"`
}