package com.example.demo;

import com.example.demo.model.BusinessItem;
import com.example.demo.service.BusinessService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 业务管理服务单元测试。
 * <p>覆盖业务新增、编辑、删除、提交、审核、驳回、归档及查询筛选核心逻辑。</p>
 */
@SpringBootTest
class BusinessServiceTest {

    @Autowired
    private BusinessService businessService;

    private BusinessItem testItem;

    @BeforeEach
    void setUp() {
        // 创建一个测试业务（草稿状态）
        BusinessItem item = new BusinessItem();
        item.setBusinessType("leave");
        item.setContent("测试请假业务");
        item.setApplicantId(2L);
        item.setApplicantName("张三");
        testItem = businessService.addBusiness(item);
    }

    /**
     * 测试新增业务。
     *
     * @requirement(name="业务新增", id="SRS-F-02-001")
     */
    @Test
    void testAddBusiness() {
        assertNotNull(testItem.getId());
        assertEquals("draft", testItem.getStatus());
        assertNotNull(testItem.getBusinessNo());
    }

    /**
     * 测试编辑业务。
     *
     * @requirement(name="业务编辑", id="SRS-F-02-002")
     */
    @Test
    void testEditBusiness() {
        testItem.setContent("修改后的业务内容");
        BusinessItem edited = businessService.editBusiness(testItem);
        assertEquals("修改后的业务内容", edited.getContent());
    }

    /**
     * 测试删除业务。
     *
     * @requirement(name="业务删除", id="SRS-F-02-003")
     */
    @Test
    void testDeleteBusiness() {
        assertDoesNotThrow(() -> businessService.deleteBusiness(testItem.getId()));
    }

    /**
     * 测试提交业务。
     *
     * @requirement(name="业务提交", id="SRS-F-02-004")
     */
    @Test
    void testSubmitBusiness() {
        BusinessItem submitted = businessService.submitBusiness(testItem.getId(), 1L, "系统管理员");
        assertEquals("submitted", submitted.getStatus());
        assertEquals(1L, submitted.getReviewerId());
    }

    /**
     * 测试审核通过业务。
     *
     * @requirement(name="业务审核", id="SRS-F-02-005")
     */
    @Test
    void testApproveBusiness() {
        // 先提交
        businessService.submitBusiness(testItem.getId(), 1L, "系统管理员");
        // 审核通过
        BusinessItem approved = businessService.approveBusiness(testItem.getId(), "同意");
        assertEquals("approved", approved.getStatus());
        assertEquals("同意", approved.getReviewComment());
    }

    /**
     * 测试驳回业务。
     *
     * @requirement(name="业务驳回", id="SRS-F-02-006")
     */
    @Test
    void testRejectBusiness() {
        businessService.submitBusiness(testItem.getId(), 1L, "系统管理员");
        BusinessItem rejected = businessService.rejectBusiness(testItem.getId(), "资料不全，请补充");
        assertEquals("rejected", rejected.getStatus());
        assertEquals("资料不全，请补充", rejected.getReviewComment());
    }

    /**
     * 测试归档业务。
     *
     * @requirement(name="业务归档", id="SRS-F-02-007")
     */
    @Test
    void testArchiveBusiness() {
        businessService.submitBusiness(testItem.getId(), 1L, "系统管理员");
        businessService.approveBusiness(testItem.getId(), "同意");
        BusinessItem archived = businessService.archiveBusiness(testItem.getId());
        assertEquals("archived", archived.getStatus());
    }

    /**
     * 测试业务查询筛选。
     *
     * @requirement(name="业务查询筛选", id="SRS-F-02-008")
     */
    @Test
    void testListBusinesses() {
        // 按类型筛选
        List<BusinessItem> results = businessService.listBusinesses("leave", null, null, null, 1, 10);
        assertFalse(results.isEmpty());
        assertTrue(results.stream().allMatch(b -> "leave".equals(b.getBusinessType())));

        // 按状态筛选
        List<BusinessItem> draftList = businessService.listBusinesses(null, "draft", null, null, 1, 10);
        assertFalse(draftList.isEmpty());
        assertTrue(draftList.stream().allMatch(b -> "draft".equals(b.getStatus())));
    }
}
