package com.ruoyi.web.modules.entry.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.entry.dto.EntryApplyShopDTO;
import com.ruoyi.web.modules.entry.dto.EntryApplySubmitDTO;
import com.ruoyi.web.modules.merchant.constant.MerchantConstants;
import com.ruoyi.web.modules.merchant.domain.BizMerchant;
import com.ruoyi.web.modules.merchant.support.BizCompleteEvaluator;

/**
 * 申请表单映射测试（MEA-UT）
 */
class EntryApplyFormMapperTest
{
    @Test
    void toFormJson_andParse_roundTrip()
    {
        EntryApplySubmitDTO dto = new EntryApplySubmitDTO();
        dto.setAgreementAccepted(true);
        BizMerchant subject = new BizMerchant();
        subject.setMerchantType(MerchantConstants.TYPE_PERSON);
        subject.setPersonName("张三");
        dto.setSubject(subject);
        BizMerchant biz = new BizMerchant();
        biz.setMerchantName("张三店");
        dto.setBiz(biz);
        EntryApplyShopDTO shop = new EntryApplyShopDTO();
        shop.setShopName("旗舰店");
        dto.setShop(shop);

        String json = EntryApplyFormMapper.toFormJson(dto);
        assertNotNull(json);

        EntryApplyFormMapper.ParsedForm parsed = EntryApplyFormMapper.parseFormJson(json);
        assertEquals("张三", parsed.getSubject().getPersonName());
        assertEquals("张三店", parsed.getBiz().getMerchantName());
        assertEquals("旗舰店", parsed.getShop().getShopName());
        assertEquals(true, parsed.getAgreementAccepted());
    }

    @Test
    void resolveSubjectLabel_person()
    {
        BizMerchant subject = new BizMerchant();
        subject.setPersonName("张三");
        assertEquals("张三", EntryApplyFormMapper.resolveSubjectLabel(MerchantConstants.TYPE_PERSON, subject));
    }

    @Test
    void toMerchantFromJson_enterprise_preservesSubjectContactAndBizComplete()
    {
        EntryApplySubmitDTO dto = buildEnterpriseSubmitDto();
        String json = EntryApplyFormMapper.toFormJson(dto);

        BizMerchant merchant = EntryApplyFormMapper.toMerchantFromJson(json, MerchantConstants.TYPE_ENTERPRISE);
        assertEquals("李四", merchant.getContactName());
        assertEquals("13900139000", merchant.getContactPhone());
        assertEquals("/profile/permit.jpg", merchant.getAccountPermit());
        assertEquals("测试企业", merchant.getMerchantName());
        assertEquals("13900139001", merchant.getServicePhone());
        assertEquals(MerchantConstants.BIZ_COMPLETE, BizCompleteEvaluator.evaluateCompleteFlag(merchant));
    }

    private EntryApplySubmitDTO buildEnterpriseSubmitDto()
    {
        EntryApplySubmitDTO dto = new EntryApplySubmitDTO();
        BizMerchant subject = new BizMerchant();
        subject.setCompanyName("测试企业");
        subject.setContactName("李四");
        subject.setContactPhone("13900139000");
        subject.setContactEmail("li@test.com");
        subject.setBankName("建设银行");
        subject.setBankBranch("海淀支行");
        subject.setBankAccount("6227000000000000");
        subject.setAccountPermit("/profile/permit.jpg");
        dto.setSubject(subject);
        BizMerchant biz = new BizMerchant();
        biz.setBizRegionCode("110105");
        biz.setBizRegionName("北京市/朝阳区");
        biz.setBizDetailAddress("经营地址1号");
        biz.setBusinessLicense("/profile/license.jpg");
        dto.setBiz(biz);
        EntryApplyShopDTO shop = new EntryApplyShopDTO();
        shop.setShopName("测试旗舰店");
        shop.setShopPhone("13900139001");
        dto.setShop(shop);
        return dto;
    }

    @Test
    void resolveSubjectLabel_enterprise()
    {
        BizMerchant subject = new BizMerchant();
        subject.setCompanyName("测试企业");
        assertEquals("测试企业",
                EntryApplyFormMapper.resolveSubjectLabel(MerchantConstants.TYPE_ENTERPRISE, subject));
    }
}
