from __future__ import annotations

import uuid
from unittest import mock

from app.utils.turn_ids import (
    clear_started_turn_message_id,
    next_turn_message_id,
    peek_started_turn_message_id,
    register_started_turn_message_id,
    reset_turn_message_sequence,
)


def test_next_turn_message_id_uses_uuid4() -> None:
    generated = uuid.UUID("12345678-1234-4234-9234-123456789abc")

    with mock.patch("app.utils.turn_ids.uuid.uuid4", return_value=generated):
        message_id = next_turn_message_id(
            {
                "callIdGed": "GED-555",
                "protocol_id": "PRT-555",
                "session_id": "session-555",
            }
        )

    assert message_id == "12345678-1234-4234-9234-123456789abc"


def test_started_turn_message_id_can_be_registered_peeked_and_cleared() -> None:
    source = {"session_id": "session-started-turn"}
    reset_turn_message_sequence(source, clear_pending=True)

    assert peek_started_turn_message_id(source) == ""

    register_started_turn_message_id(source, "message-1")
    assert peek_started_turn_message_id(source) == "message-1"

    register_started_turn_message_id(source, "message-2")
    assert peek_started_turn_message_id(source) == "message-2"

    clear_started_turn_message_id(source, "message-1")
    assert peek_started_turn_message_id(source) == "message-2"

    clear_started_turn_message_id(source, "message-2")
    assert peek_started_turn_message_id(source) == ""


def test_reset_turn_message_sequence_clears_started_turn_message_id() -> None:
    source = {"session_id": "session-reset-started-turn"}
    reset_turn_message_sequence(source, clear_pending=True)
    register_started_turn_message_id(source, "message-to-reset")

    reset_turn_message_sequence(source, clear_pending=True)

    assert peek_started_turn_message_id(source) == ""
