// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Types and validation for the `getSnapshotVersions` RPC method.

use serde::{Deserialize, Serialize};
use validator::Validate;

use super::rpc_response_context::RpcResponseContext;

#[derive(Debug, Deserialize, Serialize, Clone, Validate)]
#[serde(rename_all = "camelCase")]
pub struct GetSnapshotVersionsRequest {
    /// Maximum number of snapshots to return (1-100, default: 1)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[validate(range(min = 1, max = 100))]
    pub limit: Option<u16>,

    /// Filter for the block_height, but optional
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_height: Option<u64>,
    /// Filter for the keysvals hash, but optional.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keysvals_hash: Option<String>,

    /// Is the full file info, with block checksums, wanted?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub want_file_info: Option<bool>,
}

/// The response message for a GetSnapshotVersions request
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GetSnapshotVersionsResponse {
    pub context: RpcResponseContext,

    /// List of the versions requested
    pub versions: Vec<GetSnapshotVersionsInfo>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetSnapshotVersionsInfo {
    /// The block height in which the snapshot was taken.
    pub block_height: u64,

    /// The (hex-encoded blake3) hash of the key/val content
    pub keysvals_hash: String,

    /// time (Unix timestamp in milliseconds) when the snapshot was taken.
    pub block_time: i64,

    /// Info about the underlying files for the snapshot.
    /// Only filled in if want_file_info is set in the request.
    pub file_info: Vec<GetSnapshotVersionsFileInfo>,
}

/// Info about a particular underlying file, if want_file_info.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GetSnapshotVersionsFileInfo {
    // name of the file
    pub name: String,
    // size of the file
    pub size: u64,
    /// hashes of the different blocks (typically many MB), hex encoded.
    pub hashes: Vec<String>,
}
