from app.services.support_ticket_service import (
    ANSWERED_SUPPORT_TICKET_STATUS,
    CLOSED_SUPPORT_TICKET_STATUS,
    OPEN_SUPPORT_TICKET_STATUS,
    SupportTicketService,
)


async def test_support_ticket_lifecycle(session_factory) -> None:
    service = SupportTicketService(session_factory=session_factory)

    ticket = await service.create_ticket(
        public_id="ABCD1234",
        telegram_id=1001,
        username="tester",
        display_name="Test User",
        user_message="Need help",
        support_chat_id=-1001234567890,
        support_thread_id=12,
        support_message_id=345,
    )

    assert ticket.status == OPEN_SUPPORT_TICKET_STATUS
    assert await service.get_open_tickets_count() == 1

    answered_ticket = await service.mark_answered(ticket_id=ticket.id)
    assert answered_ticket is not None
    assert answered_ticket.status == ANSWERED_SUPPORT_TICKET_STATUS
    assert await service.get_open_tickets_count() == 1

    noted_ticket = await service.append_note(
        ticket_id=ticket.id,
        note_text="First note",
        status=OPEN_SUPPORT_TICKET_STATUS,
    )
    assert noted_ticket is not None
    assert noted_ticket.support_note == "First note"
    assert noted_ticket.status == OPEN_SUPPORT_TICKET_STATUS

    appended_ticket = await service.append_note(ticket_id=ticket.id, note_text="Second note")
    assert appended_ticket is not None
    assert appended_ticket.support_note == "First note\n\nSecond note"

    closed_ticket = await service.close_ticket(
        ticket_id=ticket.id,
        closed_by_telegram_id=9999,
    )
    assert closed_ticket is not None
    assert closed_ticket.status == CLOSED_SUPPORT_TICKET_STATUS
    assert closed_ticket.closed_by_telegram_id == 9999
    assert closed_ticket.closed_at is not None
    assert await service.get_open_tickets_count() == 0

    still_closed_ticket = await service.mark_answered(ticket_id=ticket.id)
    assert still_closed_ticket is not None
    assert still_closed_ticket.status == CLOSED_SUPPORT_TICKET_STATUS


async def test_get_ticket_by_support_message_returns_ticket(session_factory) -> None:
    service = SupportTicketService(session_factory=session_factory)

    created_ticket = await service.create_ticket(
        public_id="EFGH5678",
        telegram_id=2002,
        username=None,
        display_name="Another User",
        user_message="Another issue",
        support_chat_id=-1009876543210,
        support_thread_id=None,
        support_message_id=777,
    )

    loaded_ticket = await service.get_ticket_by_support_message(
        support_chat_id=-1009876543210,
        support_message_id=777,
    )

    assert loaded_ticket is not None
    assert loaded_ticket.id == created_ticket.id
    assert loaded_ticket.public_id == "EFGH5678"


async def test_get_admin_tickets_page_returns_latest_first(session_factory) -> None:
    service = SupportTicketService(session_factory=session_factory)

    for index in range(3):
        await service.create_ticket(
            public_id=f"TICKET{index}",
            telegram_id=3000 + index,
            username=f"user{index}",
            display_name=f"User {index}",
            user_message=f"Message {index}",
            support_chat_id=-100500,
            support_thread_id=None,
            support_message_id=900 + index,
        )

    first_page = await service.get_admin_tickets_page(page=1, page_size=2)
    second_page = await service.get_admin_tickets_page(page=2, page_size=2)

    assert first_page.total_items == 3
    assert first_page.total_pages == 2
    assert [item.public_id for item in first_page.items] == ["TICKET2", "TICKET1"]
    assert [item.public_id for item in second_page.items] == ["TICKET0"]
