use crate::model::{PackType, PackingContext};
use magnetar_model::ck;
use magnetar_sdk::types::note::{PackNoteMaybeFull, Reaction};
use magnetar_sdk::types::notification::{
    NotificationAppExt, NotificationBase, NotificationNoteExt, NotificationReactionExt,
    NotificationUserExt,
};
use magnetar_sdk::types::user::PackUserBase;

impl<'a> PackType<&'a ck::notification::Model> for NotificationBase {
    fn extract(_: &PackingContext, data: &'a ck::notification::Model) -> Self {
        NotificationBase {
            id: data.id.clone(),
            created_at: data.created_at.into(),
            is_read: data.is_read,
        }
    }
}

impl<'a> PackType<&'a PackNoteMaybeFull> for NotificationNoteExt {
    fn extract(_: &PackingContext, data: &'a PackNoteMaybeFull) -> Self {
        NotificationNoteExt { note: data.clone() }
    }
}

impl<'a> PackType<&'a PackUserBase> for NotificationUserExt {
    fn extract(_: &PackingContext, data: &'a PackUserBase) -> Self {
        NotificationUserExt { user: data.clone() }
    }
}

impl<'a> PackType<&'a Reaction> for NotificationReactionExt {
    fn extract(_: &PackingContext, data: &'a Reaction) -> Self {
        NotificationReactionExt {
            reaction: data.clone(),
        }
    }
}

impl<'a> PackType<(&'a ck::notification::Model, &'a ck::access_token::Model)>
    for NotificationAppExt
{
    fn extract(
        _: &PackingContext,
        (notification, access_token): (&'a ck::notification::Model, &'a ck::access_token::Model),
    ) -> Self {
        NotificationAppExt {
            body: notification.custom_body.clone().unwrap_or_default(),
            header: notification
                .custom_header
                .as_ref()
                .or(access_token.name.as_ref())
                .map(String::to_string)
                .clone()
                .unwrap_or_default(),
            icon: notification
                .custom_icon
                .as_ref()
                .or(access_token.icon_url.as_ref())
                .map(String::to_string)
                .clone()
                .unwrap_or_default(),
        }
    }
}
