use std::{collections::BTreeMap, fs};

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use serde::Deserialize;

/// Raw deserialization shape for a single chat type entry from `message_type.json`.
#[derive(Deserialize)]
pub struct RawChatType {
    /// Numeric ID assigned to this chat type in the vanilla registry.
    id: u32,
    //    components: ChatType,
}

// #[derive(Deserialize)]
// pub struct ChatType {
//     chat: Decoration,
//     narration: Decoration,
// }

// #[derive(Deserialize)]
// pub struct Decoration {
//     translation_key: String,
//     #[serde(default, skip_serializing_if = "Option::is_none")]
//     style: Option<Style>,
//     parameters: Vec<String>,
// }

/// Generates the `TokenStream` for message type `u8` constants, including a synthetic `RAW` variant.
pub fn build() -> TokenStream {
    let json: BTreeMap<String, RawChatType> =
        serde_json::from_str(&fs::read_to_string("../../assets/message_type.json").unwrap())
            .expect("Failed to parse message_type.json");
    let mut variants = TokenStream::new();

    for (name, typee) in &json {
        let i = typee.id as u8;
        let name = format_ident!("{}", name.to_uppercase());
        variants.extend([quote! {
            pub const #name: u8 = #i;
        }]);
    }

    let raw_id = json.len() as u8;
    variants.extend([quote! {
        pub const RAW: u8 = #raw_id; // One higher than the highest vanilla id
    }]);

    quote! {
        #variants
    }
}
