//! UC-08-F4-2 — LogTag serde round-trip preserves TagId.
//!
//! Each of the seven boundary variants round-trips through JSON and
//! postcard, asserting that the `TagId` survives the transit.

#![cfg(feature = "serde")]

use maat_sdk_types::events::LogTag;
use maat_sdk_types::types::{CombatId, PhaseId, SeatId, TagId};

fn boundary_tags(id: TagId) -> Vec<LogTag> {
    vec![
        LogTag::TurnStart { seat: Some(SeatId::from_raw(7)), id },
        LogTag::TurnEnded { seat: Some(SeatId::from_raw(7)), id },
        LogTag::RoundStart { number: 3, id },
        LogTag::RoundEnd { number: 3, id },
        LogTag::CombatStart { id_combat: CombatId::from_raw(99), id },
        LogTag::CombatEnd { id_combat: CombatId::from_raw(99), id },
        LogTag::PhaseTransition {
            from: PhaseId::new("test:a"),
            to: PhaseId::new("test:b"),
            id,
        },
    ]
}

#[test]
fn log_tag_json_roundtrip_preserves_tag_id() {
    let id = TagId::new();
    for tag in boundary_tags(id) {
        let json = serde_json::to_string(&tag).expect("serialize");
        let decoded: LogTag = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(decoded, tag);
        assert_eq!(decoded.id(), id);
    }
}

#[test]
fn log_tag_postcard_roundtrip_preserves_tag_id() {
    let id = TagId::new();
    for tag in boundary_tags(id) {
        let bytes = postcard::to_allocvec(&tag).expect("encode");
        let decoded: LogTag = postcard::from_bytes(&bytes).expect("decode");
        assert_eq!(decoded, tag);
        assert_eq!(decoded.id(), id);
    }
}

#[test]
fn log_tag_id_accessor_returns_per_variant_value() {
    let a = TagId::new();
    let b = TagId::new();
    assert_ne!(a, b);
    let ta = LogTag::TurnStart { seat: None, id: a };
    let tb = LogTag::RoundEnd { number: 1, id: b };
    assert_eq!(ta.id(), a);
    assert_eq!(tb.id(), b);
}
