import pytest
from src.modules.prompt.schemas.requests import MultiDocumentVerificationRequest, BankStatementData
from src.modules.prompt.controller import verify_multi_documents

class DummyRequest: pass
class DummySession: pass

@pytest.mark.asyncio
async def test_bank_statement_feedback(monkeypatch):
    raw = {
        "file_type": "bank_statement",
        "file_urls": ["https://example.com/stmt_may.pdf", "https://example.com/stmt_june.pdf"],
        "data": {
            "first_name": "John",
            "middle_name": "Q",
            "last_name": "Public",
            "bank_name": "Sample Bank",
            "account_type": "Checking",
            "balance": "2500.00"
        }
    }
    req = MultiDocumentVerificationRequest.from_raw(raw)

    async def fake_verify_document(file_type, payload, file_url):
        return {
            "bank_statement_results": [
                {
                    "months_covered": ["2025-05"],
                    "missing_months": ["2025-01","2025-02","2025-03","2025-04","2024-12"],
                    "balances": [
                        {"month": "2025-05", "opening_balance": "2000.00", "closing_balance": "2500.00", "total_debits": "100.00", "total_credits": "600.00", "computed_closing_balance": "2500.00", "arithmetic_consistent": True}
                    ],
                    "transactions": [
                        {"date": "2025-05-10", "description": "Deposit", "debit": None, "credit": "500.00"},
                        {"date": "2025-05-12", "description": "Card Purchase", "debit": "50.00", "credit": None}
                    ],
                    "provided_final_balance": payload.get("balance"),
                    "statement_final_balance": "2500.00",
                    "final_balance_matches": True,
                    "months_complete": False,
                    "arithmetic_inconsistencies": [],
                    "name_matches": {"first_name": True, "middle_name": True, "last_name": True},
                    "bank_name_matches": True,
                    "account_type_matches": True,
                    "errors": []
                },
                {
                    "months_covered": ["2025-06"],
                    "missing_months": ["2025-01","2025-02","2025-03","2025-04","2024-12"],
                    "balances": [
                        {"month": "2025-06", "opening_balance": "2500.00", "closing_balance": "2550.00", "total_debits": "50.00", "total_credits": "100.00", "computed_closing_balance": "2550.00", "arithmetic_consistent": True}
                    ],
                    "transactions": [
                        {"date": "2025-06-05", "description": "Deposit", "debit": None, "credit": "100.00"}
                    ],
                    "provided_final_balance": payload.get("balance"),
                    "statement_final_balance": "2550.00",
                    "final_balance_matches": False,
                    "months_complete": False,
                    "arithmetic_inconsistencies": [],
                    "name_matches": {"first_name": True, "middle_name": False, "last_name": True},
                    "bank_name_matches": True,
                    "account_type_matches": False,
                    "errors": []
                }
            ]
        }

    from src.modules.prompt import controller as controller_module
    monkeypatch.setattr(controller_module.azure_openai_client, "verify_document", fake_verify_document)

    resp = await verify_multi_documents(DummySession(), req, DummyRequest())
    assert resp.bank_statement_results is not None
    assert len(resp.bank_statement_results) == 2
    # Check feedback present per file
    for item in resp.bank_statement_results:
        assert 'feedback' in item
        fb = item['feedback']
        assert 'fully_matched' in fb and 'partially_matched' in fb and 'not_matched' in fb
    # First file should have arithmetic and final_balance fully matched
    fb1 = resp.bank_statement_results[0]['feedback']
    assert 'final_balance' in fb1['fully_matched']
    assert 'arithmetic' in fb1['fully_matched']
    # Second file should have final_balance partial and account_type not matched
    fb2 = resp.bank_statement_results[1]['feedback']
    assert 'final_balance' in fb2['partially_matched'] or 'final_balance' in fb2['not_matched']
    assert 'account_type' in fb2['not_matched']
