use crate::testing_prelude::*;
use serde_json::{from_str as json_from_str, to_string as json_to_string};

#[test]
fn timestamp_serialize_offset() {
    let timestamp = TimeStamp::from_rfc3339("2024-10-18T12:34:56+01:30").unwrap();
    let serialized = json_to_string(&timestamp).unwrap();
    assert_eq!(serialized, "\"2024-10-18T11:04:56Z\"");
}

#[test]
fn timestamp_serialize_zulu() {
    let timestamp = TimeStamp::from_rfc3339("2024-10-18T12:34:56Z").unwrap();
    let serialized = json_to_string(&timestamp).unwrap();
    assert_eq!(serialized, "\"2024-10-18T12:34:56Z\"");
}

#[test]
fn timestamp_deserialize_offset() {
    // Arrange
    let json = "\"2024-10-18T12:34:56-02:30\"";
    let deserialized: TimeStamp = json_from_str(json).unwrap();

    // Act
    let expected = TimeStamp::from_rfc3339("2024-10-18T12:34:56-02:30").unwrap();

    // Assert
    assert_eq!(deserialized, expected);
}

#[test]
fn timestamp_round_trip() {
    // Arrange
    let timestamp = TimeStamp::from_rfc3339("2024-10-18T12:34:56-02:30").unwrap();

    // Act
    let serialized = json_to_string(&timestamp).unwrap();
    let deserialized: TimeStamp = json_from_str(&serialized).unwrap();

    // Assert
    assert_eq!(timestamp, deserialized);
}

#[test]
fn timestamp_deserialize_invalid_format() {
    // Arrange
    let invalid_json = "\"invalid-date-format\"";

    // Act
    let result: Result<TimeStamp, _> = json_from_str(invalid_json);

    // Assert
    assert!(
        result.is_err(),
        "Deserialization should fail with invalid date format"
    );
}
