// 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

use crate::Url;

#[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,
)]
pub struct ConsultationListUrl(Url);

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

    use super::ConsultationListUrl;

    #[test]
    fn from_str() {
        assert_eq!(
            ConsultationListUrl(
                "https://oparl.example.org/consultation"
                    .parse()
                    .expect("value must be a parseable url")
            ),
            "https://oparl.example.org/consultation"
                .parse()
                .expect("value must be a parseable id")
        );
    }
}

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

    use super::ConsultationListUrl;

    #[test]
    fn serialize() {
        assert_eq!(
            json!(ConsultationListUrl(
                "https://oparl.example.org/consultation"
                    .parse()
                    .expect("value must be a parseable url")
            )),
            json!("https://oparl.example.org/consultation")
        );
    }

    #[test]
    fn deserialize_good() {
        let deserialized: ConsultationListUrl =
            serde_json::from_value(json!("https://oparl.example.org/consultation"))
                .expect("value must be deserializable as ConsultationListId");
        assert_eq!(
            deserialized,
            ConsultationListUrl(
                "https://oparl.example.org/consultation"
                    .parse()
                    .expect("value must be a parseable url")
            )
        );
    }

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