package com.ruoyi.info.collection.service.support;

import com.ruoyi.info.collection.domain.CcdiCreditNegativeInfo;
import com.ruoyi.info.collection.domain.CcdiDebtsInfo;
import com.ruoyi.lsfx.domain.response.CreditParsePayload;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

class CreditInfoPayloadAssemblerTest {

    private final CreditInfoPayloadAssembler assembler = new CreditInfoPayloadAssembler();

    @Test
    void shouldConvertDebtPayloadToSevenTypedRows() {
        CreditParsePayload payload = new CreditParsePayload();
        Map<String, Object> debt = new HashMap<>();
        debt.put("uncle_bank_house_bal", "50000");
        debt.put("uncle_bank_house_lmt", "100000");
        debt.put("uncle_bank_house_state", "正常");
        debt.put("uncle_not_bank_bal", "2000");
        debt.put("uncle_not_bank_lmt", "3000");
        debt.put("uncle_not_bank_state", "逾期");
        debt.put("uncle_credit_card_bal", "100");
        debt.put("uncle_credit_card_lmt", "500");
        debt.put("uncle_credit_card_state", "正常");
        payload.setLxDebt(debt);

        List<CcdiDebtsInfo> rows = assembler.buildDebts("330101199001010011", "张三", LocalDate.parse("2026-03-01"), payload);

        assertEquals(3, rows.size());
        assertEquals("住房贷款", rows.get(0).getDebtSubType());
        assertEquals("非银", rows.get(1).getCreditorType());
        assertEquals("信用卡", rows.get(2).getDebtSubType());
        assertEquals(new BigDecimal("500"), rows.get(2).getDebtTotalAmount());
    }

    @Test
    void shouldBuildNegativeInfoFromPublicTypePayload() {
        CreditParsePayload payload = new CreditParsePayload();
        Map<String, Object> publictype = new HashMap<>();
        publictype.put("civil_cnt", 2);
        publictype.put("civil_lmt", "9800");
        payload.setLxPublictype(publictype);

        CcdiCreditNegativeInfo info = assembler.buildNegative("330101199001010011", "张三", LocalDate.parse("2026-03-01"), payload);

        assertEquals(2, info.getCivilCnt());
        assertEquals(new BigDecimal("9800"), info.getCivilLmt());
    }

    @Test
    void shouldSkipDebtTypeWhenStateIsMissingSentinel() {
        CreditParsePayload payload = new CreditParsePayload();
        Map<String, Object> debt = new HashMap<>();
        debt.put("uncle_bank_house_bal", "50000");
        debt.put("uncle_bank_house_lmt", "100000");
        debt.put("uncle_bank_house_state", "-9999");
        debt.put("uncle_not_bank_bal", "2000");
        debt.put("uncle_not_bank_lmt", "3000");
        debt.put("uncle_not_bank_state", "正常");
        payload.setLxDebt(debt);

        List<CcdiDebtsInfo> rows = assembler.buildDebts("330101199001010011", "张三", LocalDate.parse("2026-03-01"), payload);

        assertEquals(1, rows.size());
        assertEquals("非银行贷款", rows.get(0).getDebtSubType());
    }

    @Test
    void shouldTreatNegativeRiskMissingSentinelAsEmptyValue() {
        CreditParsePayload payload = new CreditParsePayload();
        Map<String, Object> publictype = new HashMap<>();
        publictype.put("civil_cnt", "-9999");
        publictype.put("civil_lmt", "-9999.0");
        publictype.put("enforce_cnt", 1);
        publictype.put("enforce_lmt", "1200");
        payload.setLxPublictype(publictype);

        CcdiCreditNegativeInfo info = assembler.buildNegative("330101199001010011", "张三", LocalDate.parse("2026-03-01"), payload);

        assertEquals(0, info.getCivilCnt());
        assertNull(info.getCivilLmt());
        assertEquals(1, info.getEnforceCnt());
        assertEquals(new BigDecimal("1200"), info.getEnforceLmt());
    }

    @Test
    void shouldSkipDebtRowWhenAllMetricsAreEmpty() {
        CreditParsePayload payload = new CreditParsePayload();
        payload.setLxDebt(new HashMap<>());

        assertTrue(assembler.buildDebts("3301", "张三", LocalDate.parse("2026-03-01"), payload).isEmpty());
    }
}
