use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

use crate::party_traits::{find_fields_in_struct, is_type};

const PEER_STR: &str = "peer_ctx";

pub fn derive_peer(input: TokenStream) -> TokenStream {
    // Parsing
    let input = parse_macro_input!(input as DeriveInput);
    let peer_ctx_fields = match find_fields_in_struct(&input, [PEER_STR.to_string()]) {
        Ok(mut results) => results.remove(PEER_STR).unwrap_or_default(),
        Err(e) => return e,
    };

    // Validation
    let peer_ctx_ident = match peer_ctx_fields.as_slice() {
        [(ident, ty)] if is_type(ty, "PeerContext") => ident,
        [_] => return quote! { compile_error!("Found a field marked with #[peer_ctx] or named `peer_ctx` but not of type `PeerContext`.") }.into(),
        [] => return quote! { compile_error!("Found no field marked with #[peer_ctx] or a field named `peer_ctx`.") }.into(),
        _ => return quote! { compile_error!("Found multiple fields marked with #[peer_ctx] or named `peer_ctx`. Only one is allowed.") }.into(),
    };

    // Implementation
    let struct_name = &input.ident;
    let generics = &input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    quote! {
        impl #impl_generics network::context::Peer for #struct_name #ty_generics #where_clause {
            fn peer_context(&self) -> &network::context::PeerContext {
                &self.#peer_ctx_ident
            }
        }
    }
    .into()
}
