package dev.lions.unionflow.server.payment.pispi;

import dev.lions.unionflow.server.payment.pispi.dto.Pacs002Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class Pacs002ResponseTest {

    private static final String SAMPLE_XML = """
            <?xml version="1.0" encoding="UTF-8"?>
            <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.002.001.14">
              <FIToFIPmtStsRpt>
                <TxInfAndSts>
                  <OrgnlEndToEndId>REF-001</OrgnlEndToEndId>
                  <TxSts>ACSC</TxSts>
                  <ClrSysRef>BCEAO-12345</ClrSysRef>
                </TxInfAndSts>
              </FIToFIPmtStsRpt>
            </Document>
            """;

    @Test
    @DisplayName("fromXml parse le statut de transaction")
    void fromXml_parsesTransactionStatus() {
        Pacs002Response resp = Pacs002Response.fromXml(SAMPLE_XML);
        assertThat(resp.getTransactionStatus()).isEqualTo("ACSC");
    }

    @Test
    @DisplayName("fromXml parse le originalEndToEndId")
    void fromXml_parsesOriginalEndToEndId() {
        Pacs002Response resp = Pacs002Response.fromXml(SAMPLE_XML);
        assertThat(resp.getOriginalEndToEndId()).isEqualTo("REF-001");
    }

    @Test
    @DisplayName("fromXml parse le clearingSystemReference")
    void fromXml_parsesClearingSystemReference() {
        Pacs002Response resp = Pacs002Response.fromXml(SAMPLE_XML);
        assertThat(resp.getClearingSystemReference()).isEqualTo("BCEAO-12345");
    }

    @Test
    @DisplayName("fromXml retourne null pour les champs absents")
    void fromXml_returnsNullForMissingFields() {
        Pacs002Response resp = Pacs002Response.fromXml(SAMPLE_XML);
        assertThat(resp.getRejectReasonCode()).isNull();
        assertThat(resp.getAcceptanceDateTime()).isNull();
    }

    @Test
    @DisplayName("fromXml parse le rejectReasonCode quand présent")
    void fromXml_parsesRejectReasonCode() {
        String xml = """
                <?xml version="1.0" encoding="UTF-8"?>
                <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.002.001.14">
                  <FIToFIPmtStsRpt>
                    <TxInfAndSts>
                      <OrgnlEndToEndId>REF-002</OrgnlEndToEndId>
                      <TxSts>RJCT</TxSts>
                      <RsnCd>AC01</RsnCd>
                    </TxInfAndSts>
                  </FIToFIPmtStsRpt>
                </Document>
                """;
        Pacs002Response resp = Pacs002Response.fromXml(xml);
        assertThat(resp.getTransactionStatus()).isEqualTo("RJCT");
        assertThat(resp.getRejectReasonCode()).isEqualTo("AC01");
    }

    @Test
    @DisplayName("fromXml lève IllegalArgumentException si XML invalide")
    void fromXml_throwsOnInvalidXml() {
        assertThatThrownBy(() -> Pacs002Response.fromXml("not xml at all"))
                .isInstanceOf(IllegalArgumentException.class)
                .hasMessageContaining("pacs.002");
    }
}
