package com.gxwebsoft.glt.service;

import com.gxwebsoft.glt.entity.GltUserTicket;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class GltTicketRevokeServiceTest {

    @Mock
    private GltUserTicketService gltUserTicketService;
    @Mock
    private GltUserTicketReleaseService gltUserTicketReleaseService;
    @Mock
    private GltTicketOrderService gltTicketOrderService;
    @Mock
    private com.gxwebsoft.shop.service.ShopOrderGoodsService shopOrderGoodsService;

    @InjectMocks
    private GltTicketRevokeService gltTicketRevokeService;

    @Test
    void revokeByShopOrder_noTenant_noop() {
        int revoked = gltTicketRevokeService.revokeByShopOrder(null, 1, "O1", "r");
        assertEquals(0, revoked);
        verifyNoInteractions(gltUserTicketService, gltUserTicketReleaseService, gltTicketOrderService, shopOrderGoodsService);
    }

    @Test
    void revokeByShopOrder_noTickets_noop() {
        when(gltUserTicketService.list(any())).thenReturn(List.of());
        when(shopOrderGoodsService.list(any())).thenReturn(List.of());
        int revoked = gltTicketRevokeService.revokeByShopOrder(10584, 1, "O1", "r");
        assertEquals(0, revoked);
        verify(gltUserTicketService, times(1)).list(any());
        verify(shopOrderGoodsService, times(1)).list(any());
        verifyNoMoreInteractions(gltUserTicketService, shopOrderGoodsService);
        verifyNoInteractions(gltUserTicketReleaseService, gltTicketOrderService);
    }

    @Test
    void revokeByShopOrder_hasTickets_revokesAll() {
        GltUserTicket t = new GltUserTicket();
        t.setId(123);
        t.setTenantId(10584);
        when(gltUserTicketService.list(any())).thenReturn(List.of(t));
        when(gltUserTicketService.update(isNull(), any())).thenReturn(true);
        when(gltTicketOrderService.update(isNull(), any())).thenReturn(true);
        when(gltUserTicketReleaseService.update(isNull(), any())).thenReturn(true);

        int revoked = gltTicketRevokeService.revokeByShopOrder(10584, 1, "O1", "退款撤销");

        assertEquals(1, revoked);
        verify(gltTicketOrderService, times(1)).update(isNull(), any());
        verify(gltUserTicketReleaseService, times(1)).update(isNull(), any());
        verify(gltUserTicketService, times(1)).update(isNull(), any());
        verifyNoInteractions(shopOrderGoodsService);
    }
}
