package com.sino.mci.channel.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sino.mci.channel.dto.ChannelAccountEventRequest;
import com.sino.mci.channel.entity.ChannelAccount;
import com.sino.mci.channel.repository.ChannelAccountMapper;
import com.sino.mci.channel.repository.ChannelEventRecordMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
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;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Sql(scripts = "/sql/test-channel-account.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
class ChannelAccountEventControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ChannelAccountMapper accountMapper;

    @Autowired
    private ChannelEventRecordMapper eventRecordMapper;

    @Test
    void shouldUpdateAccountOnHeartbeat() throws Exception {
        ChannelAccountEventRequest request = new ChannelAccountEventRequest();
        request.setEventId("evt-001");
        request.setEventType("heartbeat");
        request.setAccountId("wx-bot-01");
        request.setTenantCode("rescue");
        request.setData(Map.of(
                "login_status", "success",
                "online_status", "online",
                "risk_level", 10));

        mockMvc.perform(post("/msg-platform/api/v1/channel-account/event")
                        .contextPath("/msg-platform")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));

        ChannelAccount account = accountMapper.selectById(1L);
        assertThat(account.getOnlineStatus()).isEqualTo(1);
        assertThat(account.getRiskLevel()).isEqualTo(10);
        assertThat(eventRecordMapper.selectById("evt-001")).isNotNull();
    }

    @Test
    void shouldIgnoreDuplicateEvent() throws Exception {
        ChannelAccountEventRequest request = new ChannelAccountEventRequest();
        request.setEventId("evt-dup-001");
        request.setEventType("heartbeat");
        request.setAccountId("wx-bot-01");
        request.setTenantCode("rescue");
        request.setData(Map.of("online_status", "online"));

        mockMvc.perform(post("/msg-platform/api/v1/channel-account/event")
                        .contextPath("/msg-platform")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk());

        mockMvc.perform(post("/msg-platform/api/v1/channel-account/event")
                        .contextPath("/msg-platform")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk());

        assertThat(eventRecordMapper.selectCount(null)).isEqualTo(1);
    }
}
