package com.njcn.gather.aireport.reportapproval.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.gather.aireport.reportapproval.handler.ReportApprovalHandler;
import com.njcn.gather.aireport.reportapproval.handler.ReportApprovalHandlerRegistry;
import com.njcn.gather.aireport.reportapproval.mapper.ReportApprovalMapper;
import com.njcn.gather.aireport.reportapproval.pojo.constant.ReportApprovalConst;
import com.njcn.gather.aireport.reportapproval.pojo.param.ReportApprovalParam;
import com.njcn.gather.aireport.reportapproval.pojo.vo.ReportApprovalPendingVO;
import org.junit.Assert;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ReportApprovalServiceImplTest {

    @Test
    public void pendingPageShouldUseDatabasePagingResult() {
        ReportApprovalMapper mapper = mock(ReportApprovalMapper.class);
        ReportApprovalHandlerRegistry registry = new ReportApprovalHandlerRegistry(Collections.<ReportApprovalHandler>emptyList());
        ReportApprovalServiceImpl service = new ReportApprovalServiceImpl(mapper, registry);
        ReportApprovalParam.PendingQueryParam param = new ReportApprovalParam.PendingQueryParam();
        param.setPageNum(2);
        param.setPageSize(5);

        ReportApprovalPendingVO record = new ReportApprovalPendingVO();
        record.setReportType(ReportApprovalConst.REPORT_TYPE_REPORT_MODEL);
        record.setReportId("model-001");
        record.setReportName("模板A");
        record.setSubmitterId("user-001");
        record.setSubmitterName("张三");
        record.setSubmitTime(LocalDateTime.of(2026, 6, 29, 10, 0, 0));
        List<ReportApprovalPendingVO> records = Arrays.asList(record);
        when(mapper.countPendingReports(param)).thenReturn(11L);
        when(mapper.selectPendingPage(5L, 5L, param)).thenReturn(records);

        Page<ReportApprovalPendingVO> page = service.pendingPage(param);

        Assert.assertEquals(2L, page.getCurrent());
        Assert.assertEquals(5L, page.getSize());
        Assert.assertEquals(11L, page.getTotal());
        Assert.assertEquals(1, page.getRecords().size());
        Assert.assertEquals("user-001", page.getRecords().get(0).getSubmitterId());
        Assert.assertEquals("张三", page.getRecords().get(0).getSubmitterName());
        verify(mapper).countPendingReports(param);
        verify(mapper).selectPendingPage(5L, 5L, param);
    }

    @Test
    public void passShouldDelegateToMatchedHandler() {
        ReportApprovalMapper mapper = mock(ReportApprovalMapper.class);
        ReportApprovalHandler handler = mock(ReportApprovalHandler.class);
        when(handler.getReportType()).thenReturn(ReportApprovalConst.REPORT_TYPE_TEST_REPORT);
        when(handler.audit(org.mockito.ArgumentMatchers.any(ReportApprovalParam.AuditParam.class), eq(true))).thenReturn(true);
        ReportApprovalHandlerRegistry registry = new ReportApprovalHandlerRegistry(Arrays.asList(handler));
        ReportApprovalServiceImpl service = new ReportApprovalServiceImpl(mapper, registry);
        ReportApprovalParam.AuditParam param = new ReportApprovalParam.AuditParam();
        param.setReportType(ReportApprovalConst.REPORT_TYPE_TEST_REPORT);
        param.setReportId("report-001");
        param.setApprovalReason("通过");

        boolean result = service.pass(param);

        Assert.assertTrue(result);
        verify(handler).audit(param, true);
    }
}
