package com.yhy.module.core.service.quota;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.yhy.module.core.controller.admin.quota.vo.QuotaRateFieldLabelRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateFieldLabelSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaRateFieldLabelDO;
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 定额费率字段标签字典 Service 测试
 *
 * @author yhy
 */
@SpringBootTest
public class QuotaRateFieldLabelServiceTest {

    @Resource
    private QuotaRateFieldLabelService fieldLabelService;

    /**
     * 测试创建字段标签
     */
    @Test
    public void testCreateFieldLabel() {
        // 准备数据
        QuotaRateFieldLabelSaveReqVO createReqVO = new QuotaRateFieldLabelSaveReqVO();
        createReqVO.setCatalogItemId(1002L); // 模式节点ID
        createReqVO.setLabelName("土石方工程");

        // 执行创建
        Long labelId = fieldLabelService.createFieldLabel(createReqVO);

        // 验证结果
        assertNotNull(labelId);
        System.out.println("创建标签成功，ID: " + labelId);

        // 查询验证
        QuotaRateFieldLabelDO label = fieldLabelService.getFieldLabel(labelId);
        assertNotNull(label);
        assertEquals("土石方工程", label.getLabelName());
        assertEquals(1002L, label.getCatalogItemId());
    }

    /**
     * 测试更新字段标签
     */
    @Test
    public void testUpdateFieldLabel() {
        // 先创建
        QuotaRateFieldLabelSaveReqVO createReqVO = new QuotaRateFieldLabelSaveReqVO();
        createReqVO.setCatalogItemId(1002L);
        createReqVO.setLabelName("土石方工程");
        Long labelId = fieldLabelService.createFieldLabel(createReqVO);

        // 更新
        QuotaRateFieldLabelSaveReqVO updateReqVO = new QuotaRateFieldLabelSaveReqVO();
        updateReqVO.setId(labelId);
        updateReqVO.setCatalogItemId(1002L);
        updateReqVO.setLabelName("土方工程");
        fieldLabelService.updateFieldLabel(updateReqVO);

        // 验证
        QuotaRateFieldLabelDO label = fieldLabelService.getFieldLabel(labelId);
        assertEquals("土方工程", label.getLabelName());
        System.out.println("更新标签成功，新名称: " + label.getLabelName());
    }

    /**
     * 测试查询标签列表
     */
    @Test
    public void testGetFieldLabelList() {
        // 创建多个标签
        Long catalogItemId = 1002L;
        
        QuotaRateFieldLabelSaveReqVO label1 = new QuotaRateFieldLabelSaveReqVO();
        label1.setCatalogItemId(catalogItemId);
        label1.setLabelName("土石方工程");
        fieldLabelService.createFieldLabel(label1);

        QuotaRateFieldLabelSaveReqVO label2 = new QuotaRateFieldLabelSaveReqVO();
        label2.setCatalogItemId(catalogItemId);
        label2.setLabelName("地基处理工程");
        fieldLabelService.createFieldLabel(label2);

        // 查询列表
        List<QuotaRateFieldLabelRespVO> labels = fieldLabelService.getFieldLabelList(catalogItemId);

        // 验证
        assertNotNull(labels);
        assertTrue(labels.size() >= 2);
        System.out.println("查询到标签数量: " + labels.size());
        labels.forEach(label -> System.out.println("  - " + label.getLabelName()));
    }

    /**
     * 测试交换排序
     */
    @Test
    public void testSwapSort() {
        // 创建两个标签
        Long catalogItemId = 1002L;
        
        QuotaRateFieldLabelSaveReqVO label1 = new QuotaRateFieldLabelSaveReqVO();
        label1.setCatalogItemId(catalogItemId);
        label1.setLabelName("标签A");
        Long labelId1 = fieldLabelService.createFieldLabel(label1);

        QuotaRateFieldLabelSaveReqVO label2 = new QuotaRateFieldLabelSaveReqVO();
        label2.setCatalogItemId(catalogItemId);
        label2.setLabelName("标签B");
        Long labelId2 = fieldLabelService.createFieldLabel(label2);

        // 获取初始排序
        QuotaRateFieldLabelDO before1 = fieldLabelService.getFieldLabel(labelId1);
        QuotaRateFieldLabelDO before2 = fieldLabelService.getFieldLabel(labelId2);
        System.out.println("交换前 - 标签A排序: " + before1.getSortOrder() + ", 标签B排序: " + before2.getSortOrder());

        // 交换排序
        fieldLabelService.swapSort(labelId1, labelId2);

        // 验证排序已交换
        QuotaRateFieldLabelDO after1 = fieldLabelService.getFieldLabel(labelId1);
        QuotaRateFieldLabelDO after2 = fieldLabelService.getFieldLabel(labelId2);
        System.out.println("交换后 - 标签A排序: " + after1.getSortOrder() + ", 标签B排序: " + after2.getSortOrder());

        assertEquals(before1.getSortOrder(), after2.getSortOrder());
        assertEquals(before2.getSortOrder(), after1.getSortOrder());
    }

    /**
     * 测试删除标签（未被使用）
     */
    @Test
    public void testDeleteFieldLabel() {
        // 创建标签
        QuotaRateFieldLabelSaveReqVO createReqVO = new QuotaRateFieldLabelSaveReqVO();
        createReqVO.setCatalogItemId(1002L);
        createReqVO.setLabelName("临时标签");
        Long labelId = fieldLabelService.createFieldLabel(createReqVO);

        // 删除
        fieldLabelService.deleteFieldLabel(labelId);

        // 验证已删除
        QuotaRateFieldLabelDO label = fieldLabelService.getFieldLabel(labelId);
        assertNull(label);
        System.out.println("删除标签成功");
    }
}
