use super::electrs::BlockInfo;
use crate::{
    common::{
        hash::Hash256,
        inscriptions::{AddressHash, Genesis, InscriptionNumber, Outpoint},
        mime,
        tokens::InscriptionProtocol,
    },
    utils::sha256::IntoHash256,
};
use rand::Rng;

/// Block data that content will pull from dataswamp
#[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
pub struct ContentBlockData {
    pub block_info: BlockInfo,
    pub inscriptions: Vec<ContentInscriptionData>,
    pub moves: Vec<ContentMoveData>,
}

#[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
pub struct ContentMoveData {
    pub is_creation: bool,
    pub from: AddressHash,
    pub to: AddressHash,
    pub number: InscriptionNumber,
}

#[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
pub struct ContentInscriptionData {
    pub content_type: mime::Extension,
    // pub content: Vec<u8>,
    pub content_hash: Hash256,
    pub content_len: usize,
    pub genesis: Genesis,
    pub number: InscriptionNumber,
    pub genesis_fee: u64,
    pub protocol: Option<InscriptionProtocol>,
}

impl ContentInscriptionData {
    pub fn randomized(num: InscriptionNumber) -> Self {
        let mut rnd = rand::thread_rng();
        let genesis = Genesis(Outpoint::randomized());
        let genesis_fee = rnd.gen_range(0..1_000_000);
        let content = rnd.gen_range(0..1_000_000).to_string().into_bytes();

        Self {
            content_type: mime::Extension::Other(mime::Other::Bytes),
            content_hash: content.hash_256(),
            content_len: content.len(),
            genesis,
            number: num,
            genesis_fee,
            protocol: None,
        }
    }
}
