package com.njcn.rdms.module.project.service.workreport.common;

import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.project.constant.WorkReportConstants;
import com.njcn.rdms.module.project.dal.dataobject.workreport.monthly.MonthlyReportApprovalRecordDO;
import com.njcn.rdms.module.project.dal.dataobject.workreport.monthly.MonthlyReportDO;
import com.njcn.rdms.module.project.dal.mysql.workreport.monthly.MonthlyReportApprovalRecordMapper;
import com.njcn.rdms.module.project.dal.mysql.workreport.monthly.MonthlyReportMapper;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.ArgumentCaptor;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class WorkReportCommonServicePeriodKeyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private WorkReportCommonService workReportCommonService;

    @Mock
    private MonthlyReportMapper monthlyReportMapper;
    @Mock
    private MonthlyReportApprovalRecordMapper monthlyReportApprovalRecordMapper;

    @Test
    void listMonthlyReportsForSignOff_whenPeriodMonth_shouldConvertToMonthlyPeriodKey() {
        List<Long> reporterIds = List.of(1001L, 1002L);
        String periodMonth = "2026-06";
        String expectedPeriodKey = "monthly-2026-06-01-2026-06-30";
        MonthlyReportDO report = new MonthlyReportDO();
        report.setId(3001L);
        List<MonthlyReportDO> expectedReports = List.of(report);
        when(monthlyReportMapper.selectListByReporterIdsAndPeriodKey(reporterIds, expectedPeriodKey,
                List.of(WorkReportConstants.STATUS_APPROVED))).thenReturn(expectedReports);

        List<MonthlyReportDO> actualReports = workReportCommonService.listMonthlyReportsForSignOff(reporterIds, periodMonth);

        assertSame(expectedReports, actualReports);
        verify(monthlyReportMapper).selectListByReporterIdsAndPeriodKey(reporterIds, expectedPeriodKey,
                List.of(WorkReportConstants.STATUS_APPROVED));
    }

    @Test
    void syncPerformanceResult_whenPeriodMonth_shouldConvertToMonthlyPeriodKeyBeforeLookup() {
        Long reporterId = 1001L;
        String periodMonth = "2026-06";
        String expectedPeriodKey = "monthly-2026-06-01-2026-06-30";
        String performanceResult = "95";

        MonthlyReportDO report = new MonthlyReportDO();
        report.setId(3001L);
        MonthlyReportApprovalRecordDO approvalRecord = new MonthlyReportApprovalRecordDO();
        approvalRecord.setId(4001L);

        when(monthlyReportMapper.selectByReporterIdAndPeriodKey(reporterId, expectedPeriodKey)).thenReturn(report);
        when(monthlyReportApprovalRecordMapper.selectLatestApprovedByMonthlyReportId(report.getId()))
                .thenReturn(approvalRecord);

        workReportCommonService.syncPerformanceResult(reporterId, periodMonth, performanceResult);

        ArgumentCaptor<MonthlyReportApprovalRecordDO> updateCaptor =
                ArgumentCaptor.forClass(MonthlyReportApprovalRecordDO.class);
        verify(monthlyReportApprovalRecordMapper).updateById(updateCaptor.capture());
        assertEquals(approvalRecord.getId(), updateCaptor.getValue().getId());
        assertEquals(performanceResult, updateCaptor.getValue().getPerformanceResult());
    }
}
