// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

#[derive(
    Debug,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    derive_more::AsRef,
    derive_more::Display,
    derive_more::From,
    derive_more::FromStr,
    derive_more::Into,
    serde::Serialize,
    serde::Deserialize,
)]
#[cfg_attr(feature = "sea-orm", derive(sea_orm::DeriveValueType))]
pub struct Post(String);

impl From<&str> for Post {
    fn from(value: &str) -> Self {
        Self(value.into())
    }
}

impl Post {
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::Post;

    #[test]
    fn from_str() {
        assert_eq!(
            Post("Vorsitzender".to_string()),
            "Vorsitzender"
                .parse()
                .expect("value must be a parseable post")
        );
    }
}

#[cfg(test)]
mod serde_tests {
    use pretty_assertions::assert_eq;
    use serde_json::json;

    use super::Post;

    #[test]
    fn serialize() {
        assert_eq!(
            json!(Post("Vorsitzender".to_string())),
            json!("Vorsitzender")
        );
    }

    #[test]
    fn deserialize_good() {
        let deserialized: Post = serde_json::from_value(json!("Vorsitzender"))
            .expect("value must be deserializable as post");
        assert_eq!(deserialized, Post::from("Vorsitzender"));
    }

    #[test]
    fn deserialize_bad() {
        assert!(serde_json::from_value::<Post>(json!([])).is_err());
        assert!(serde_json::from_value::<Post>(json!({})).is_err());
        assert!(serde_json::from_value::<Post>(json!(true)).is_err());
        assert!(serde_json::from_value::<Post>(json!(123)).is_err());
    }
}
