package com.sino.mci.inbound.intent;

import com.sino.mci.admin.repository.TenantMapper;
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.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class IntentRecognitionServiceTest {

    @Mock
    private TenantMapper tenantMapper;

    @InjectMocks
    private IntentRecognitionService intentRecognitionService;

    @Test
    void shouldRecognizeCreateOrderByRule() {
        when(tenantMapper.selectOne(any())).thenReturn(null);

        IntentResult result = intentRecognitionService.recognize("test", "我要下单", Map.of());

        assertThat(result.getIntent()).isEqualTo("create_order");
        assertThat(result.getConfidence()).isEqualTo(1.0);
    }

    @Test
    void shouldExtractCustomerNameSlot() {
        when(tenantMapper.selectOne(any())).thenReturn(null);

        IntentResult result = intentRecognitionService.recognize(
                "test", "我要下单，客户张三，地址北京", Map.of());

        assertThat(result.getIntent()).isEqualTo("create_order");
        assertThat(result.getSlots())
                .containsEntry("customer_name", "张三")
                .containsEntry("address", "北京");
        assertThat(result.getMissingSlots()).contains("phone");
        assertThat(result.isRequiresConfirmation()).isTrue();
    }

    @Test
    void shouldReturnUnknownWhenNoRuleMatchesAndLlmNotConfigured() {
        when(tenantMapper.selectOne(any())).thenReturn(null);

        IntentResult result = intentRecognitionService.recognize(
                "test", "今天天气不错", Map.of());

        assertThat(result.getIntent()).isEqualTo("unknown");
        assertThat(result.getConfidence()).isEqualTo(0.0);
        assertThat(result.getSlots()).isEmpty();
        assertThat(result.getMissingSlots()).isEmpty();
        assertThat(result.isRequiresConfirmation()).isFalse();
    }
}
