syntax = "proto3";

package raft;

// Raft consensus service
service Raft {
    // RequestVote RPC (§5.2)
    rpc RequestVote(RequestVoteRequest) returns (RequestVoteResponse);
    
    // AppendEntries RPC (§5.3) - also used for heartbeat
    rpc AppendEntries(AppendEntriesRequest) returns (AppendEntriesResponse);
    
    // InstallSnapshot RPC (§7)
    rpc InstallSnapshot(InstallSnapshotRequest) returns (InstallSnapshotResponse);
}

// RequestVote RPC
message RequestVoteRequest {
    uint64 term = 1;
    uint64 candidate_id = 2;
    uint64 last_log_index = 3;
    uint64 last_log_term = 4;
}

message RequestVoteResponse {
    uint64 term = 1;
    bool vote_granted = 2;
    uint64 from_id = 3;
}

// AppendEntries RPC
message AppendEntriesRequest {
    uint64 term = 1;
    uint64 leader_id = 2;
    uint64 prev_log_index = 3;
    uint64 prev_log_term = 4;
    repeated LogEntry entries = 5;
    uint64 leader_commit = 6;
}

message AppendEntriesResponse {
    uint64 term = 1;
    bool success = 2;
    uint64 match_index = 3;
    uint64 from_id = 4;
}

message LogEntry {
    uint64 term = 1;
    uint64 index = 2;
    bytes command = 3;  // Serialized command
}

// InstallSnapshot RPC
message InstallSnapshotRequest {
    uint64 term = 1;
    uint64 leader_id = 2;
    uint64 last_included_index = 3;
    uint64 last_included_term = 4;
    bytes data = 5;
    bytes checksum = 6;
}

message InstallSnapshotResponse {
    uint64 term = 1;
    bool success = 2;
    uint64 from_id = 3;
}

// ============================================================================
// Control Plane Service
// ============================================================================

service ControlPlane {
    // Returns current status of the Raft node
    rpc GetNodeStatus(Empty) returns (NodeStatus);
    // Returns recent log entries (for TUI/Dashboard)
    rpc GetRecentLogs(LogRequest) returns (LogResponse);
}

message Empty {}

message NodeStatus {
    string state = 1;          // Leader, Follower, Candidate
    uint64 current_term = 2;
    uint64 commit_index = 3;
    uint64 last_applied = 4;
    uint32 connected_peers = 5;
    uint64 id = 6;
}

message LogRequest {
    uint32 limit = 1;
}

message LogResponse {
    repeated string logs = 1;
}
