"""Microsoft Dynamics 365 Finance & Operations — the field table.

Dynamics splits the same business fact across modules, so a purchase order lives under
`PurchId` while the vendor invoice that answers it lives under `InvoiceId` and the packing
slip under `PackingSlipId`. The priority lists collapse that back into one canonical
`document_no`, and `DataAreaId` — the legal-entity code — plays the part SAP calls `BUKRS`.
"""

from __future__ import annotations

from .base import ErpAdapter, FieldMap

DYNAMICS_365_FO = FieldMap(
    system_id="MS-DYNAMICS-365-FO",
    company_code=("DataAreaId", "LegalEntityId", "dataAreaId"),
    document_no=("PurchId", "SalesId", "InvoiceId", "PackingSlipId", "VoucherId", "AgreementId"),
    fiscal_year=("FiscalYear", "FiscalCalendarYear"),
    doc_date=("DocumentDate", "InvoiceDate", "AccountingDate", "TransDate", "DeliveryDate"),
    amount=("InvoiceAmount", "LineAmount", "AmountCur", "TotalAmount"),
    currency=("CurrencyCode", "TransactionCurrencyCode"),
    doc_type=("DocumentType", "PurchaseType", "NEOEDGE_DOCTYPE"),
    lines=("Lines", "InvoiceLines", "PurchaseLines", "items"),
    line_sku=("ItemId", "ProductNumber", "sku"),
    line_quantity=("Qty", "PurchQty", "ReceivedQty", "quantity"),
    line_unit=("UnitId", "UnitOfMeasure", "unit"),
    line_unit_price=("UnitPrice", "PurchPrice", "unit_price"),
    doc_kinds={
        "PurchaseOrder": "PURCHASE_ORDER",
        "OrderConfirmation": "ORDER_CONFIRMATION",
        "PackingSlip": "DELIVERY_NOTE",
        "ProductReceipt": "GOODS_RECEIPT",
        "VendorInvoice": "INVOICE",
        "CustomerInvoice": "INVOICE",
        "VendorPayment": "PAYMENT",
        "CreditNote": "CREDIT_MEMO",
        "PurchaseAgreement": "CONTRACT",
    },
)


def adapter() -> ErpAdapter:
    return ErpAdapter(DYNAMICS_365_FO)
