package com.infoepoch.pms.agent.platform.chat.adapter.in.web;

import com.infoepoch.pms.agent.platform.chat.application.ChatService;
import com.infoepoch.pms.agent.platform.chat.application.dto.SessionResponse;
import com.infoepoch.pms.agent.platform.chat.domain.Session;
import com.infoepoch.pms.agent.platform.chatbot.domain.Chatbot;
import com.infoepoch.pms.agent.platform.shared.exception.GlobalExceptionHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(MockitoExtension.class)
class ChatControllerTest {

    @Mock
    private ChatService chatService;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp() {
        mockMvc = MockMvcBuilders.standaloneSetup(new ChatController(chatService))
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
    }

    @Test
    void shouldExposeThreadStateFromContextResetEndpoints() throws Exception {
        SessionResponse response = new SessionResponse(session("session-1", "session-1", true));
        when(chatService.markContextResetPending("session-1")).thenReturn(response);
        when(chatService.clearContextResetPending("session-1")).thenReturn(new SessionResponse(session("session-1", "session-1", false)));

        mockMvc.perform(post("/api/chat/sessions/session-1/context-reset")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data.activeThreadId").value("session-1"))
                .andExpect(jsonPath("$.data.contextResetPending").value(true));

        mockMvc.perform(delete("/api/chat/sessions/session-1/context-reset"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data.activeThreadId").value("session-1"))
                .andExpect(jsonPath("$.data.contextResetPending").value(false));
    }

    private Session session(String id, String activeThreadId, boolean contextResetPending) {
        LocalDateTime now = LocalDateTime.now();
        Chatbot chatbot = Chatbot.restore(
                "chatbot-1",
                "客服助手",
                "desc",
                null,
                null,
                List.of(),
                20,
                true,
                now,
                now,
                Set.of()
        );
        return Session.restore(id, "user-1", chatbot, Session.STATUS_ACTIVE, null, activeThreadId,
                contextResetPending, now, now);
    }
}
