use super::{Contact, Scene};
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct GroupContact {
	/// 事件来源
	pub scene: Scene,
	/// 群聊id
	pub peer: String,
	/// 群名称
	pub name: Option<String>,
}

impl Contact for GroupContact {
	fn scene(&self) -> Scene {
		Scene::Group
	}

	fn peer(&self) -> &str {
		self.peer.as_str()
	}

	fn name(&self) -> Option<&str> {
		self.name.as_deref()
	}

	fn is_friend(&self) -> bool {
		false
	}

	fn is_group(&self) -> bool {
		true
	}
}

/// 构建群聊事件
///
/// ## 参数
/// `peer`: 群聊id
/// `name`: 群聊昵称（可选）
///
/// ## 示例
/// ```rust
/// use puniyu_types::contact_group;
/// let contact = contact_group!("2233");
/// ```
#[cfg(feature = "contact")]
#[macro_export]
macro_rules! contact_group {
    ( $( $key:ident : $value:expr ),* $(,)? ) => {{
        let mut contact = $crate::contact::GroupContact {
            scene: $crate::contact::Scene::Group,
            ..$crate::contact::GroupContact::default()
        };
        $(
            contact.$key = contact_group!(@convert $key, $value);
        )*
        contact
    }};

    ($peer:expr, $name:expr) => {{
        let mut contact = $crate::contact::GroupContact {
            scene: $crate::contact::Scene::Group,
            ..$crate::contact::GroupContact::default()
        };
        contact.peer = $peer.to_string();
        contact.name = Some($name.to_string());
        contact
    }};

    ($peer:expr) => {{
        let mut contact = $crate::contact::GroupContact {
            scene: $crate::contact::Scene::Group,
            ..$crate::contact::GroupContact::default()
        };
        contact.peer = $peer.to_string();
        contact
    }};

    (@convert peer, $v:expr) => { $v.to_string() };
    (@convert name, None) => { None };
    (@convert name, $v:expr) => { Some($v.to_string()) };
}
