#!/usr/bin/env python3
"""
Tests for ISO-20022 Parser
"""

import pytest
import xml.etree.ElementTree as ET
from parsers.iso20022_parser import ISO20022Parser

@pytest.fixture
def parser():
    return ISO20022Parser()

@pytest.fixture
def sample_pacs008():
    return """<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.10">
    <FIToFICstmrCdtTrf>
        <GrpHdr>
            <MsgId>MSG001</MsgId>
            <CreDtTm>2024-01-01T00:00:00</CreDtTm>
            <NbOfTxs>1</NbOfTxs>
        </GrpHdr>
        <CdtTrfTxInf>
            <PmtId>
                <InstrId>INSTR001</InstrId>
                <EndToEndId>E2E001</EndToEndId>
                <TxId>TX001</TxId>
            </PmtId>
            <PmtTpInf>
                <SvcLvl>
                    <Cd>SEPA</Cd>
                </SvcLvl>
            </PmtTpInf>
            <Amt>
                <InstdAmt Ccy="USD">1000.00</InstdAmt>
            </Amt>
            <CdtrAgt>
                <FinInstnId>
                    <BIC>BANKUS33</BIC>
                </FinInstnId>
            </CdtrAgt>
            <Cdtr>
                <Nm>Creditor Name</Nm>
                <PstlAdr>
                    <StrtNm>Street</StrtNm>
                    <BldgNb>123</BldgNb>
                    <PstCd>12345</PstCd>
                    <TwnNm>City</TwnNm>
                    <Ctry>US</Ctry>
                </PstlAdr>
            </Cdtr>
            <CdtrAcct>
                <Id>
                    <IBAN>US1234567890</IBAN>
                </Id>
            </CdtrAcct>
            <RmtInf>
                <Ustrd>Remittance information</Ustrd>
            </RmtInf>
        </CdtTrfTxInf>
    </FIToFICstmrCdtTrf>
</Document>"""

def test_parse_pacs008(parser, sample_pacs008):
    """Test parsing pacs.008 message"""
    result = parser.parse_pacs008(sample_pacs008)
    
    assert result['type'] == 'pacs.008'
    assert result['messageId'] == 'MSG001'
    assert result['currency'] == 'USD'
    assert result['amount'] == '1000.00'
    assert 'creditor' in result
    assert 'debtor' in result

def test_detect_message_type(parser, sample_pacs008):
    """Test message type detection"""
    root = ET.fromstring(sample_pacs008)
    message_type = parser._detect_message_type(root)
    assert message_type == 'pacs.008'

def test_extract_party(parser, sample_pacs008):
    """Test party extraction"""
    root = ET.fromstring(sample_pacs008)
    creditor = parser._extract_party(root, './/pacs:Cdtr')
    assert 'name' in creditor
    assert creditor['name'] == 'Creditor Name'

def test_extract_account(parser, sample_pacs008):
    """Test account extraction"""
    root = ET.fromstring(sample_pacs008)
    account = parser._extract_account(root, './/pacs:CdtrAcct')
    assert 'id' in account

if __name__ == '__main__':
    pytest.main([__file__])

