package com.ruoyi.app.user;

import com.ruoyi.app.user.dto.MerchantSubaccountStatusRequest;
import com.ruoyi.app.user.dto.MerchantSubaccountView;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MessageUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class MerchantSubaccountAdminControllerTest {

    private MerchantSubaccountApplicationService applicationService;
    private MerchantSubaccountAdminController controller;
    private MockedStatic<MessageUtils> messages;

    @BeforeEach
    void setUp() {
        messages = mockStatic(MessageUtils.class);
        messages.when(() -> MessageUtils.message(org.mockito.ArgumentMatchers.anyString()))
                .thenAnswer(invocation -> invocation.getArgument(0));
        applicationService = mock(MerchantSubaccountApplicationService.class);
        controller = new MerchantSubaccountAdminController(applicationService);
    }

    @AfterEach
    void tearDown() {
        messages.close();
    }

    @Test
    void listsOnlySubaccountsResolvedForSpecifiedOwner() {
        MerchantSubaccountView view = new MerchantSubaccountView();
        view.setUserId(8L);
        when(applicationService.listForPlatform(1L)).thenReturn(List.of(view));

        AjaxResult result = controller.list(1L);

        assertEquals(List.of(view), result.get(AjaxResult.DATA_TAG));
        verify(applicationService).listForPlatform(1L);
    }

    @Test
    void platformStatusEndpointDoesNotChangeOwnerStatus() {
        MerchantSubaccountStatusRequest request = new MerchantSubaccountStatusRequest();
        request.setEnabled(false);

        controller.updatePlatformStatus(8L, request);

        verify(applicationService).updatePlatformStatus(8L, request);
    }
}
