use betex::{
    book::protocol::{
        command::{Command, CommandKind},
        reject::RejectReason,
    },
    book::{Book, BookEvent, BookEventEnvelope, EventMetadata},
    types::*,
};

#[test]
fn pre_event_only_rejects_live_phase_command() {
    let market_id = MarketId(1);
    let mut book = Book::new_with_kind(
        market_id,
        MarketKind::PreEventOnly,
        [RunnerId(1), RunnerId(2)],
    );

    let cmd = Command {
        correlation_id: Some(CorrelationId(1.to_string())),
        metadata: None,
        market_id,
        kind: CommandKind::SetMarketPhase {
            phase: MarketPhase::Live,
        },
    };

    let err = book
        .handle(&cmd)
        .expect_err("pre-event-only market must reject live phase");
    assert_eq!(err.reason, RejectReason::MarketInPlayNotSupported);
}

#[test]
#[should_panic(expected = "pre-event-only market cannot be applied into live phase")]
fn pre_event_only_panics_if_live_phase_event_is_applied() {
    let market_id = MarketId(1);
    let mut book = Book::new_with_kind(
        market_id,
        MarketKind::PreEventOnly,
        [RunnerId(1), RunnerId(2)],
    );

    let env = BookEventEnvelope {
        market_id,
        market_name: String::new(),
        market_seq: 1,
        timestamp: unix_epoch(),
        metadata: EventMetadata::default(),
        event: BookEvent::MarketPhaseChanged {
            to: MarketPhase::Live,
            reason: "TEST".to_string(),
        },
    };

    book.apply_event(&env);
}
