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

import java.math.BigDecimal;

public class Pacs008Request {

    private String messageId;
    private String creationDateTime;
    private String numberOfTransactions;
    private String endToEndId;
    private String instrId;
    private BigDecimal amount;
    private String currency;
    private String debtorName;
    private String debtorBic;
    private String creditorName;
    private String creditorBic;
    private String creditorIban;
    private String remittanceInfo;

    public Pacs008Request() {}

    public String getMessageId() { return messageId; }
    public void setMessageId(String messageId) { this.messageId = messageId; }

    public String getCreationDateTime() { return creationDateTime; }
    public void setCreationDateTime(String creationDateTime) { this.creationDateTime = creationDateTime; }

    public String getNumberOfTransactions() { return numberOfTransactions; }
    public void setNumberOfTransactions(String numberOfTransactions) { this.numberOfTransactions = numberOfTransactions; }

    public String getEndToEndId() { return endToEndId; }
    public void setEndToEndId(String endToEndId) { this.endToEndId = endToEndId; }

    public String getInstrId() { return instrId; }
    public void setInstrId(String instrId) { this.instrId = instrId; }

    public BigDecimal getAmount() { return amount; }
    public void setAmount(BigDecimal amount) { this.amount = amount; }

    public String getCurrency() { return currency; }
    public void setCurrency(String currency) { this.currency = currency; }

    public String getDebtorName() { return debtorName; }
    public void setDebtorName(String debtorName) { this.debtorName = debtorName; }

    public String getDebtorBic() { return debtorBic; }
    public void setDebtorBic(String debtorBic) { this.debtorBic = debtorBic; }

    public String getCreditorName() { return creditorName; }
    public void setCreditorName(String creditorName) { this.creditorName = creditorName; }

    public String getCreditorBic() { return creditorBic; }
    public void setCreditorBic(String creditorBic) { this.creditorBic = creditorBic; }

    public String getCreditorIban() { return creditorIban; }
    public void setCreditorIban(String creditorIban) { this.creditorIban = creditorIban; }

    public String getRemittanceInfo() { return remittanceInfo; }
    public void setRemittanceInfo(String remittanceInfo) { this.remittanceInfo = remittanceInfo; }

    public String toXml() {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10\">\n" +
                "  <FIToFICstmrCdtTrf>\n" +
                "    <GrpHdr>\n" +
                "      <MsgId>" + escape(messageId) + "</MsgId>\n" +
                "      <CreDtTm>" + escape(creationDateTime) + "</CreDtTm>\n" +
                "      <NbOfTxs>1</NbOfTxs>\n" +
                "    </GrpHdr>\n" +
                "    <CdtTrfTxInf>\n" +
                "      <PmtId>\n" +
                "        <InstrId>" + escape(instrId) + "</InstrId>\n" +
                "        <EndToEndId>" + escape(endToEndId) + "</EndToEndId>\n" +
                "      </PmtId>\n" +
                "      <IntrBkSttlmAmt Ccy=\"" + escape(currency) + "\">" + (amount != null ? amount.toPlainString() : "0") + "</IntrBkSttlmAmt>\n" +
                "      <Dbtr><Nm>" + escape(debtorName) + "</Nm></Dbtr>\n" +
                "      <DbtrAgt><FinInstnId><BICFI>" + escape(debtorBic) + "</BICFI></FinInstnId></DbtrAgt>\n" +
                "      <Cdtr><Nm>" + escape(creditorName) + "</Nm></Cdtr>\n" +
                "      <CdtrAgt><FinInstnId><BICFI>" + escape(creditorBic) + "</BICFI></FinInstnId></CdtrAgt>\n" +
                "      <RmtInf><Ustrd>" + escape(remittanceInfo) + "</Ustrd></RmtInf>\n" +
                "    </CdtTrfTxInf>\n" +
                "  </FIToFICstmrCdtTrf>\n" +
                "</Document>";
    }

    private static String escape(String value) {
        if (value == null) return "";
        return value
                .replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&apos;");
    }
}
