/*
 *     Copyright 2022 The Dragonfly Authors
 *
 * 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.
 */

syntax = "proto3";

package dfdaemon.v2;

import "pkg/apis/common/v2/common.proto";
import "google/protobuf/empty.proto";
import "validate/validate.proto";

option go_package = "d7y.io/api/v2/pkg/apis/dfdaemon/v2;dfdaemon";

// GetPieceNumbersRequest represents gets piece numbers request of GetPieceNumbers.
message GetPieceNumbersRequest {
  // Task id.
  string task_id = 1 [(validate.rules).string.min_len = 1];
}

// GetPieceNumbersResponse represents gets piece numbers response of GetPieceNumbers.
message GetPieceNumbersResponse {
  // Piece numbers.
  repeated int32 piece_numbers = 1;
}

// InterestedPiecesRequest represents interested pieces request of SyncPiecesRequest.
message InterestedPiecesRequest {
  // Interested piece numbers.
  repeated int32 piece_numbers = 1 [(validate.rules).repeated = {min_items: 1}];
}

// SyncPiecesRequest represents request of AnnouncePeer.
message SyncPiecesRequest{
  // Task id.
  string task_id = 1 [(validate.rules).string.min_len = 1];

  oneof request {
    option (validate.required) = true;

    InterestedPiecesRequest interested_pieces_request = 2;
  }
}

// InterestedPiecesResponse represents interested pieces response of SyncPiecesResponse.
message InterestedPiecesResponse {
  // Interested piece of task.
  common.v2.Piece piece = 1 [(validate.rules).message.required = true];
}

// SyncPiecesResponse represents response of SyncPieces.
message SyncPiecesResponse {
  oneof response {
    option (validate.required) = true;

    InterestedPiecesResponse interested_pieces_response = 1;
  }
}

// DownloadTaskRequest represents request of DownloadTask.
message DownloadTaskRequest {
  // Download information.
  common.v2.Download download = 1 [(validate.rules).message.required = true];
}

// UploadTaskRequest represents request of UploadTask.
message UploadTaskRequest {
  // Task metadata.
  common.v2.Task task = 1 [(validate.rules).message.required = true];
}

// StatTaskRequest represents request of StatTask.
message StatTaskRequest {
  // Task id.
  string task_id = 1 [(validate.rules).string.min_len = 1];
}

// DeleteTaskRequest represents request of DeleteTask.
message DeleteTaskRequest {
  // Task id.
  string task_id = 1 [(validate.rules).string.min_len = 1];
}

// Dfdaemon RPC Service.
service Dfdaemon {
  // GetPieceNumbers gets piece numbers from the other peer.
  rpc GetPieceNumbers(GetPieceNumbersRequest) returns(GetPieceNumbersResponse);

  // SyncPieces syncs pieces from the other peer.
  rpc SyncPieces(stream SyncPiecesRequest)returns(stream SyncPiecesResponse);

  // DownloadTask downloads task back-to-source.
  rpc DownloadTask(DownloadTaskRequest) returns(google.protobuf.Empty);

  // UploadTask uploads task to p2p network.
  rpc UploadTask(UploadTaskRequest) returns(google.protobuf.Empty);

  // StatTask stats task information.
  rpc StatTask(StatTaskRequest) returns(common.v2.Task);

  // DeleteTask deletes task from p2p network.
  rpc DeleteTask(DeleteTaskRequest) returns(google.protobuf.Empty);
}
