// SPDX-License-Identifier: CC0-1.0

//! Types for methods found under the `== Util ==` section of the API docs.
//!
//! These structs model the types returned by the JSON-RPC API but have concrete types
//! and are not specific to a specific version of Bitcoin Core.

use bitcoin::address::NetworkUnchecked;
use bitcoin::{sign_message, Address, FeeRate, ScriptBuf, WitnessProgram, WitnessVersion};
use serde::{Deserialize, Serialize};

/// Models the result of JSON-RPC method `createmultisig`.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CreateMultisig {
    /// The value of the new multisig address.
    pub address: Address<NetworkUnchecked>,
    /// The string value of the hex-encoded redemption script.
    pub redeem_script: ScriptBuf,
    /// The descriptor for this multisig. v0.20 and later only.
    pub descriptor: Option<String>,
    /// Any warnings resulting from the creation of this multisig. v23 and later only.
    pub warnings: Option<Vec<String>>,
}

/// Models the result of JSON-RPC method `deriveaddresses`.
///
/// > deriveaddresses "descriptor" ( range )
/// >
/// > Derives one or more addresses corresponding to an output descriptor.
/// > Returns an array of derived addresses.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DeriveAddresses {
    /// The derived addresses.
    pub addresses: Vec<Address<NetworkUnchecked>>,
}

/// Models the result of JSON-RPC method `deriveaddresses` for multipath descriptors.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DeriveAddressesMultipath {
    /// The derived addresses for each of the multipath expansions of the descriptor, in multipath specifier order.
    pub addresses: Vec<DeriveAddresses>,
}

/// Models the result of JSON-RPC method `estimatesmartfee`.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct EstimateSmartFee {
    /// Estimate fee rate in BTC/kB.
    pub fee_rate: Option<FeeRate>,
    /// Errors encountered during processing.
    pub errors: Option<Vec<String>>,
    /// Block number where estimate was found.
    pub blocks: u32,
}

/// Models the result of JSON-RPC method `signmessagewithprivkey`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignMessageWithPrivKey(pub sign_message::MessageSignature);

/// Models the result of JSON-RPC method `validateaddress`.
// TODO: Support serde (currently not supported by `WitnessProgram` or `WitnessVersion`)
// https://github.com/rust-bitcoin/rust-bitcoin/issues/3513
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValidateAddress {
    /// If the address is valid or not. If not, this is the only property returned.
    pub is_valid: bool,
    /// The bitcoin address validated.
    pub address: Address<NetworkUnchecked>,
    /// The hex encoded scriptPubKey generated by the address.
    pub script_pubkey: ScriptBuf,
    /// If the key is a script.
    pub is_script: bool,
    /// If the address is a witness address.
    pub is_witness: bool,
    /// The version number of the witness program.
    pub witness_version: Option<WitnessVersion>,
    /// The hex value of the witness program.
    pub witness_program: Option<WitnessProgram>,
}
