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.*;

/**
 * 业务数据管理服务的单元测试。
 * 验证业务数据新增、编辑、删除、提交、审核、驳回、归档和查询筛选功能。
 */
@SpringBootTest
class BusinessServiceTest {

    @Autowired
    private BusinessService businessService;

    private BusinessItem testItem;

    @BeforeEach
    void setUp() {
        // 使用一个全新的测试数据
        testItem = new BusinessItem();
        testItem.setTitle("测试业务数据");
        testItem.setContent("这是测试内容");
        testItem.setType("test");
        testItem.setCreatedBy(1L);
    }

    /**
     * 测试新增业务数据。
     *
     * @requirement(name="业务新增", id="SRS-IMS_F-010")
     */
    @Test
    void testCreateBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        assertNotNull(created);
        assertNotNull(created.getId());
        assertEquals("draft", created.getStatus());
        assertEquals("测试业务数据", created.getTitle());
    }

    /**
     * 测试编辑业务数据。
     *
     * @requirement(name="业务编辑", id="SRS-IMS_F-011")
     */
    @Test
    void testUpdateBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        created.setTitle("更新后的标题");
        BusinessItem updated = businessService.updateBusinessItem(created.getId(), created);
        assertEquals("更新后的标题", updated.getTitle());
    }

    /**
     * 测试删除业务数据。
     *
     * @requirement(name="业务删除", id="SRS-IMS_F-012")
     */
    @Test
    void testDeleteBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        assertDoesNotThrow(() -> businessService.deleteBusinessItem(created.getId()));
    }

    /**
     * 测试提交业务数据至审核。
     *
     * @requirement(name="业务提交", id="SRS-IMS_F-013")
     */
    @Test
    void testSubmitBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        BusinessItem submitted = businessService.submitBusinessItem(created.getId());
        assertEquals("pending", submitted.getStatus());
    }

    /**
     * 测试审核通过业务数据。
     *
     * @requirement(name="业务审核", id="SRS-IMS_F-014")
     */
    @Test
    void testApproveBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        businessService.submitBusinessItem(created.getId());
        BusinessItem approved = businessService.approveBusinessItem(created.getId());
        assertEquals("approved", approved.getStatus());
    }

    /**
     * 测试驳回业务数据。
     *
     * @requirement(name="业务驳回", id="SRS-IMS_F-015")
     */
    @Test
    void testRejectBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        businessService.submitBusinessItem(created.getId());
        BusinessItem rejected = businessService.rejectBusinessItem(created.getId());
        assertEquals("rejected", rejected.getStatus());
    }

    /**
     * 测试归档业务数据。
     *
     * @requirement(name="业务归档", id="SRS-IMS_F-016")
     */
    @Test
    void testArchiveBusinessItem() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        businessService.submitBusinessItem(created.getId());
        businessService.approveBusinessItem(created.getId());
        BusinessItem archived = businessService.archiveBusinessItem(created.getId());
        assertEquals("archived", archived.getStatus());
    }

    /**
     * 测试多条件组合查询。
     *
     * @requirement(name="查询筛选", id="SRS-IMS_F-017")
     */
    @Test
    void testSearchBusinessItems() {
        businessService.createBusinessItem(testItem);

        // 按标题搜索
        List<BusinessItem> results = businessService.searchBusinessItems("测试", null, null, null, 0, 10);
        assertNotNull(results);
        assertTrue(results.size() > 0);

        // 按类型搜索
        results = businessService.searchBusinessItems(null, "test", null, null, 0, 10);
        assertNotNull(results);
        assertTrue(results.size() > 0);

        // 按状态搜索
        results = businessService.searchBusinessItems(null, null, "draft", null, 0, 10);
        assertNotNull(results);

        // 按创建人搜索
        results = businessService.searchBusinessItems(null, null, null, 1L, 0, 10);
        assertNotNull(results);
    }

    /**
     * 测试状态流转异常：编辑非草稿数据。
     *
     * @requirement(name="业务编辑", id="SRS-IMS_F-011")
     */
    @Test
    void testUpdateNonDraftItemThrowsException() {
        BusinessItem created = businessService.createBusinessItem(testItem);
        businessService.submitBusinessItem(created.getId());
        BusinessItem finalItem = created;
        assertThrows(IllegalStateException.class, () -> {
            businessService.updateBusinessItem(finalItem.getId(), finalItem);
        });
    }
}
