import pytest
import os
import asyncio
from app.modules.banking.application.parsers.acb_excel_parser import ACBExcelParser
from app.modules.banking.application.parsers.vpbank_excel_parser import VPBankExcelParser
from app.modules.banking.application.parsers.tcb_pdf_parser import TCBPdfParser
from app.modules.banking.application.reconciliation_engine import ReconciliationEngine

# Hardcode sample paths for testing
SAMPLE_DIR = r"C:\Projects\DSCons\Finance\UNC-2026"
ACB_FILE = os.path.join(SAMPLE_DIR, "ACB-2026", "13456888888_SAOKE_TK_202602.xlsx")
VPB_FILE = os.path.join(SAMPLE_DIR, "VPBank 2026", "AccountStmt_20260508183117.xls")
TCB_FILE = os.path.join(SAMPLE_DIR, "Techcombank-2026", "xxxxxxxxxx8888_202601_MONTHLY.pdf")

def test_acb_parser():
    if not os.path.exists(ACB_FILE):
        pytest.skip(f"Sample file {ACB_FILE} not found")
        
    parser = ACBExcelParser()
    with open(ACB_FILE, "rb") as f:
        transactions = parser.parse(f, "13456888888_SAOKE_TK_202602.xlsx")
        
    assert len(transactions) > 0
    assert "transaction_date" in transactions[0]
    assert transactions[0]["debit_amount_vnd"] >= 0
    
def test_vpbank_parser():
    if not os.path.exists(VPB_FILE):
        pytest.skip(f"Sample file {VPB_FILE} not found")
        
    parser = VPBankExcelParser()
    with open(VPB_FILE, "rb") as f:
        transactions = parser.parse(f, "AccountStmt_20260508183117.xls")
        
    assert len(transactions) > 0
    assert transactions[0]["credit_amount_vnd"] >= 0
    
def test_tcb_pdf_parser():
    if not os.path.exists(TCB_FILE):
        pytest.skip(f"Sample file {TCB_FILE} not found")
        
    parser = TCBPdfParser(password="39912136")
    with open(TCB_FILE, "rb") as f:
        transactions = parser.parse(f, "xxxxxxxxxx8888_202601_MONTHLY.pdf")
        
    assert len(transactions) > 0
    
def test_reconciliation_engine():
    engine = ReconciliationEngine(db_client=None)
    mock_txs = [
        {"description": "Thanh toan HD000123 cong ty X", "credit_amount_vnd": 500000},
        {"description": "CONG TY A chuyen tien", "credit_amount_vnd": 100000}
    ]
    
    suggested = asyncio.run(engine.suggest_matches(mock_txs))
    
    assert "000123" in suggested[0]["suggested_invoice_number"]
    assert suggested[0]["confidence_score"] >= 0.5
    assert "A" in suggested[1]["suggested_partner_name"].upper()

