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

import com.njcn.gather.aireport.reportmodel.component.ReportModelFileStorageService;
import com.njcn.gather.aireport.reportmodel.component.ReportModelUserNameResolver;
import com.njcn.gather.aireport.reportmodel.pojo.constant.ReportModelConst;
import com.njcn.gather.aireport.reportmodel.pojo.param.ReportModelParam;
import com.njcn.gather.aireport.reportmodel.pojo.po.ReportModelPO;
import org.junit.Assert;
import org.junit.Test;

import java.time.LocalDateTime;

import static org.mockito.Mockito.mock;

public class ReportModelServiceImplTest {

    @Test
    public void submitAuditShouldResetAuditFieldsWhenMoveToAuditing() {
        ReportModelPO model = new ReportModelPO();
        model.setId("model-001");
        model.setState(ReportModelConst.STATE_EDITING);
        model.setStatus(ReportModelConst.STATUS_NORMAL);
        model.setCheckId("checker-001");
        model.setCheckTime(LocalDateTime.of(2026, 6, 26, 9, 30, 0));
        model.setCheckResult("已有审核意见");

        TestableReportModelServiceImpl service = new TestableReportModelServiceImpl(model);
        ReportModelParam.SubmitAuditParam param = new ReportModelParam.SubmitAuditParam();
        param.setId("model-001");

        boolean result = service.submitAudit(param);

        Assert.assertTrue(result);
        Assert.assertEquals(ReportModelConst.STATE_AUDITING, model.getState());
        Assert.assertNull(model.getCheckId());
        Assert.assertNull(model.getCheckTime());
        Assert.assertNull(model.getCheckResult());
        Assert.assertSame(model, service.getUpdatedModel());
        Assert.assertNotNull(model.getUpdateTime());
    }

    private static class TestableReportModelServiceImpl extends ReportModelServiceImpl {

        private final ReportModelPO model;
        private ReportModelPO updatedModel;

        private TestableReportModelServiceImpl(ReportModelPO model) {
            super(mock(ReportModelFileStorageService.class), mock(ReportModelUserNameResolver.class));
            this.model = model;
        }

        @Override
        public ReportModelPO requireNormal(String id) {
            return model;
        }

        @Override
        public boolean updateById(ReportModelPO entity) {
            this.updatedModel = entity;
            return true;
        }

        private ReportModelPO getUpdatedModel() {
            return updatedModel;
        }
    }
}
