package com.ruoyi.info.collection.service;

import com.ruoyi.info.collection.domain.CcdiBizIntermediary;
import com.ruoyi.info.collection.domain.dto.CcdiIntermediaryRelativeAddDTO;
import com.ruoyi.info.collection.domain.dto.CcdiIntermediaryPersonAddDTO;
import com.ruoyi.info.collection.mapper.CcdiBizIntermediaryMapper;
import com.ruoyi.info.collection.mapper.CcdiEnterpriseBaseInfoMapper;
import com.ruoyi.info.collection.mapper.CcdiIntermediaryEnterpriseRelationMapper;
import com.ruoyi.info.collection.mapper.CcdiIntermediaryMapper;
import com.ruoyi.info.collection.service.impl.CcdiIntermediaryServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.redis.core.RedisTemplate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CcdiIntermediaryServiceImplTest {

    @InjectMocks
    private CcdiIntermediaryServiceImpl service;

    @Mock
    private CcdiBizIntermediaryMapper bizIntermediaryMapper;

    @Mock
    private CcdiEnterpriseBaseInfoMapper enterpriseBaseInfoMapper;

    @Mock
    private CcdiIntermediaryMapper intermediaryMapper;

    @Mock
    private CcdiIntermediaryEnterpriseRelationMapper enterpriseRelationMapper;

    @Mock
    private ICcdiIntermediaryPersonImportService personImportService;

    @Mock
    private ICcdiIntermediaryEntityImportService entityImportService;

    @Mock
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void insertIntermediaryPerson_shouldForceBenrenAndClearRelatedNumId() {
        CcdiIntermediaryPersonAddDTO addDTO = new CcdiIntermediaryPersonAddDTO();
        addDTO.setName("测试中介");
        addDTO.setPersonId("320101199001010011");
        addDTO.setPersonSubType("配偶");
        addDTO.setRelatedNumId("parent-id");

        when(bizIntermediaryMapper.selectCount(any())).thenReturn(0L);
        when(bizIntermediaryMapper.insert(any(CcdiBizIntermediary.class))).thenReturn(1);

        int result = service.insertIntermediaryPerson(addDTO);

        assertEquals(1, result);
        ArgumentCaptor<CcdiBizIntermediary> captor = ArgumentCaptor.forClass(CcdiBizIntermediary.class);
        verify(bizIntermediaryMapper).insert(captor.capture());
        assertEquals("本人", captor.getValue().getPersonSubType());
        assertNull(captor.getValue().getRelatedNumId());
        assertEquals("MANUAL", captor.getValue().getDataSource());
    }

    @Test
    void insertIntermediaryRelative_shouldRejectBenrenSubType() {
        CcdiBizIntermediary owner = new CcdiBizIntermediary();
        owner.setBizId("biz-1");
        owner.setPersonSubType("本人");

        CcdiIntermediaryRelativeAddDTO addDTO = new CcdiIntermediaryRelativeAddDTO();
        addDTO.setName("测试亲属");
        addDTO.setPersonId("320101199001010022");
        addDTO.setPersonSubType("本人");

        when(bizIntermediaryMapper.selectById("biz-1")).thenReturn(owner);

        RuntimeException exception = assertThrows(RuntimeException.class,
                () -> service.insertIntermediaryRelative("biz-1", addDTO));

        assertEquals("亲属关系不能为本人", exception.getMessage());
        verify(bizIntermediaryMapper, never()).insert(any(CcdiBizIntermediary.class));
    }

    @Test
    void deleteIntermediaryByIds_shouldDeleteRelativesAndEnterpriseRelationsWhenRemovingOwner() {
        CcdiBizIntermediary owner = new CcdiBizIntermediary();
        owner.setBizId("biz-1");
        owner.setPersonSubType("本人");

        when(bizIntermediaryMapper.selectById("biz-1")).thenReturn(owner);
        when(bizIntermediaryMapper.delete(any())).thenReturn(2);
        when(enterpriseRelationMapper.delete(any())).thenReturn(1);
        when(bizIntermediaryMapper.deleteById("biz-1")).thenReturn(1);

        int result = service.deleteIntermediaryByIds(new String[]{"biz-1"});

        assertEquals(1, result);
        verify(bizIntermediaryMapper).delete(any());
        verify(enterpriseRelationMapper).delete(any());
        verify(bizIntermediaryMapper).deleteById("biz-1");
    }
}
